Hi, I need to make the laser visible to everyone, that when you shoot the laser beam all the players can see it, how can I do that?
Example, I shoot the laser → all players can see the laser shot.
Any idea how to make this possible?
Localscript:
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 = game.ReplicatedStorage:FindFirstChild("LaserEvent")
RemoteEvent:FireServer(Humanoid)
end
end)
end)
The laser would have to be created on the server, whether by a remote event or another method, it’d be within a server script. Creating the laser in a local script will result in only the client being able to see the laser.
You could send necessary information to the server through a remote event and allow the server to create that laser given the data.
In the attached excerpt of code, you’re creating a laser. The problem is, the laser is being created on the client. What you could do is fire a remote event to the server and recreate the entire process (creating the ray, instancing new parts, setting properties) on the server instead of the client. Basically, that block of code would be run within a serverscript.
Additionally, you would be able to remove the event you used to slow a player down and instead slow them directly from the server.
But in this way I’ll have two instances, one on the client and one on the server, how could I create the whole process on the server and delete the one that what is on the client?
If you’re trying to make client-sided hit detection, then you could send the laser data (where the laser was shot from, and the direction of the shot) to the server, and then the server sends that data to all clients except for you. And then all the clients recreate the laser from the data they got from the server (shot origin and direction). The laser movement might be visually inaccurate because of other clients’ ping
If you want a more secure but delayed hit detection, then you would just send the origin and direction to the server, and then the server makes the laser and does all the raycasting. (Basically what @archisaurus suggested)