Hello, I need to make the laserbeam fire but it doesn’t work, I have it connected to a fireserver and I don’t understand what’s wrong…
How do I need it to work
Laser beam → click → if it hits a humanoid then the WalkSpeed and RunSpeed should be set to 0, so that they can’t move.
Locascript:
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
script.Parent.RemoteEvent:FireServer()
end
end)
end)
Well you define the “Tip” on the 4th line, therefore it will run almost instantly after the player joins the game. Because of this, there is a high chance that the tip hasn’t loaded in, maybe try changing line 4 to:
Any errors? Also what I would assume is the problem is that this is on the client of the player with the gun. When they change the walk speed of the player they hit, because it is on the client, the walk speed won’t be changed for the other player. A player’s movement is handled on their own client, so if the player’s client doesn’t see the change then they will just move the same as normal. Try changing the speed on the server with a remote event.
I don’t believe that there is anything called run speed. Just delete that line, and change the walk speed on the server instead of the client, and it should work!
script.Parent.RemoteEvent.OnServerEvent:Connect(function()
print("Is working...")
local player = game.Players.LocalPlayer
local char = player.CharacterAdded:wait()
local h = char.Humanoid
h.WalkSpeed = 0
end)
Close, but the problem is “local player” doesn’t work on a server script. The only reason it works for local scripts is that the local script is a descendant of the player, so the script knows which player is the “local player”. But on the server, the server has no idea which player you are referring to. What you want to do, is when you fire the remote event, have the first parameter be the humanoid.
Local Script:
local Humanoid = ---
local RemoteEvent = --
RemoteEvent:FireServer(Humanoid)
I did it and it didn’t work
The print that I have on the server does not appear and it does not give me any error
local script:
local Debris = game:GetService("Debris")
local LaserGun = script.Parent
local Tip = LaserGun:WaitForChild("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
end
local Humanoid = HitHumanoid
local RemoteEvent = script.Parent:FindFirstChild("RemoteEvent")
RemoteEvent:FireServer(Humanoid)
end
end)
end)
server script:
script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, h)
h.WalkSpeed = 0
print("Speed is 0")
end)
Well you are firing a different remote event on the client than you are on the server. Put the remote event in ReplicatedStorage, and use the same one in both scripts.