Laser Gun 2.0
LocalScript
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local laserEvent = rs:WaitForChild("LaserEvent")
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
tool.Sound:Play()
laserEvent:FireServer(tool, mouse.Hit.p, "Damage")
end)
end)
ServerScript
local rs = game:GetService("ReplicatedStorage")
local laserEvent = rs:WaitForChild("LaserEvent")
laserEvent.OnServerEvent:Connect(function(player, gun, mousePos, effect)
local rayOrigin = gun.Handle.CFrame.p
local rayDirection = (mousePos - gun.Handle.CFrame.p).unit * 300
local raycastParms = RaycastParams.new()
raycastParms.FilterDescendantsInstances = {player.Parent}
raycastParms.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParms)
if raycastResult then
local hitPart = raycastResult.Instance
local beam = Instance.new("Part", workspace)
if effect == "Heal" then
beam.BrickColor = BrickColor.new("Lime green")
elseif effect == "Damage" then
beam.BrickColor = BrickColor.new("Bright red")
end
beam.Material = Enum.Material.Neon
beam.Transparency = 0.25
beam.Anchored = true
beam.CanCollide = false
local distance = (gun.Handle.CFrame.p - hitPart.Position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(gun.Handle.CFrame.p, mousePos) * CFrame.new(0,0, -distance/2)
local humanoid = hitPart.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = hitPart.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
local objDistance = (player.Character.HumanoidRootPart.Position - humanoid.RootPart.Position).magnitude
if objDistance < 100 then
local damageAmount = 100 - (math.floor(objDistance/10) * 10)
if effect == "Heal" then
if humanoid.Health + damageAmount <= 100 then
humanoid:TakeDamage(-damageAmount)
else
humanoid.Health = 100
end
elseif effect == "Damage" then
humanoid:TakeDamage(damageAmount)
end
end
end
game:GetService("Debris"):AddItem(beam, 0.1)
end
end)
LocalScript - For Key Swap
Don't forget to update local script on gun too!
local label = script.Parent
local shotType = label.ShotType
label.Text = shotType.Value
shotType.Changed:Connect(function()
label.Text = shotType.Value
end)
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.V then
if shotType.Value == "Damage" then
shotType.Value = "Heal"
label.TextColor3 = Color3.new(0, 1, 0)
else
shotType.Value = "Damage"
label.TextColor3 = Color3.new(1, 0, 0)
end
end
end
end)