Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my gun not work server sided?

So I made a script, and it works. However, I tested it me shooting another player. It shows that it does damage on my screen. However, it does not deal damage on theirs. Please help! Here is the code, the code was done on a local script:

local Debris = game:GetService("Debris")

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

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

local GunDamage = 30


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("Fossil")
		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:TakeDamage(GunDamage)
			end
		end
	end)
end)

Please help! Like is there an easy video or anything?

1 answer

Local script means its only for that player that changes happen, it doesnt replicate to the server

You are gonna need remote events

then using that event u fire a function inside a script that does the actual dmg so that it replicates to all players

edit:

in the mouse.Button1Down event put remoteEvent:FireServer(HitHumanoid) and in the script u put

remoteEvent.OnServerEvent:Connect(function(player,enemy)

enemy:TakeDamage(10) -- or whatever
end)

4 comments

0

I know that, but I have no idea how to use them. Perhaps you can modify the script to fire the event and help write the event code. I have been stumped on this for so long.

0

u gonna need a client to server event, click the link i posted there is lots of examples there how u do it

0

Bruh. I know that, but i don't know when to fire the event and how to make the script actually work. Perhaps you could elaborate on that?

0

ill add it as edit

This discussion is preserved as it was. New answers, comments, and votes are closed.