What's wrong with my tool "Laser Beam"?

Hello I would like to know what is wrong with my tool, I need that when firing the LaserBeam and if it hits a humanoid then the WalkSpeed and RunSpeed should be set to 0, so that they can’t move… :frowning:

image

image

LocalScript called “LaserGunScript”:

local Debris = game:GetService("Debris")

local LaserGun = script.Parent
local Tip = LaserGun.Tip

local Player = game.Players.LocalPlayer
local Character = Player.Character


LaserGun.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		local Laser = Ray.new(Tip.CFrame.p, (Mouse.Hit.p - Tip.CFrame.p).unit * 300)
		local HitPart, HitPosition = game.Workspace:FindPartOnRay(Laser, Character, false, true)

		local LaserBeam = Instance.new("Part", game.Workspace)
		LaserBeam.BrickColor = BrickColor.new("New Yeller")
		LaserBeam.FormFactor = "Custom"
		LaserBeam.Material = "Neon"
		LaserBeam.Transparency = 0.25
		LaserBeam.Anchored = true
		LaserBeam.CanCollide = false

		local LaserDistance = (Tip.CFrame.p - HitPosition).Magnitude
		LaserBeam.Size = Vector3.new(0.3, 0.3, LaserDistance)
		LaserBeam.CFrame = CFrame.new(Tip.CFrame.p, HitPosition) * CFrame.new(0, 0, -LaserDistance/2)

		Debris:AddItem(LaserBeam, 0.1)

		if HitPart then
			local HitHumanoid = HitPart.Parent:FindFirstChild("Humanoid")

			if not HitHumanoid then
				HitHumanoid = HitPart.Parent.Parent:FindFirstChild("Humanoid")
			end

			if HitHumanoid then
				HitHumanoid.WalkSpeed = 0
				HitHumanoid.RunSpeed = 0
			end
		end
	end)
end)
2 Likes

So I’ve encountered this problem a few times. This is in a LocalScript, which means it’s only local to the player and the other player doesn’t experience it. You need to use a RemoteEvent to do it for both players or the whole game.

1 Like

Could you tell us what is going wrong?

I need to make the laser beam fire when the player click :frowning: how could i do that?

You are trying to make the gun fire on the client. You need the ray and the humanoid WalkSpeed change to be in a normal script. The information about the where the player clicked, as well as the signal to fire the weapon, should be passed from the client to the server with a RemoteEvent.

1 Like