Server Script is Killing all the Players instead of Killing 1 Only!

I have a Spinning Part that is being cloned on a local script every time a player joins.
I did that because I noticed some lag if the part is on the Server. The part is supposed to kill every player that touches it. The problem is that when a player touches the part, a remote event is fired from the client to the server and when the remote event is fired, it kills all the players instead of the player that touches the part only.

Local Script:

game.ReplicatedStorage:WaitForChild("SpinningParts"):Clone().Parent = workspace.ObbyMap["Stage 10Special"]
game.ReplicatedStorage:WaitForChild("SpinnerS6"):Clone().Parent = workspace.ObbyMap["Stage 6Special"]

function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	local p = game.Players:GetPlayerFromCharacter(part.Parent)
	if p and h then
		print(p.Name)
		game.ReplicatedStorage:WaitForChild("DamagePlayerOnS6"):FireServer(p)
	end
end

game.workspace.ObbyMap["Stage 6Special"].SpinnerS6.Lava.Touched:Connect(onTouched)
game.Workspace.ObbyMap["Stage 6Special"].SpinnerS6.Lava1.Touched:Connect(onTouched)	

Server Script:

game.ReplicatedStorage:WaitForChild("DamagePlayerOnS6").OnServerEvent:Connect(function(player)
	player.Character.Humanoid:TakeDamage(5000)
	print(player.Name)
end)

The first argument of a RemoteEvent callback will always be the player who called it. You should have a second argument on that connected function that’s gonna be what the client is sending.

1 Like

Yep like @RicoFox said it’s should be the second parameter.

game.ReplicatedStorage:WaitForChild("DamagePlayerOnS6").OnServerEvent:Connect(function(wrongPlayer,player)
	player.Character.Humanoid:TakeDamage(5000)
	print(player.Name)
end)

Also the remote is exploitable an exploiter can fire a signal to kill everyone. Might be better to just use a server side touched event or kill the player locally.

Common problem with a local touched event. Other players can trigger other players local touched events:

It triggers the remote for every player and hence kills all players.

1 Like

So what should I add as a second argument? because I did not add or use any other arguments in a remote event before.

Just have the local script check if the touching player is the LocalPlayer, and then fire and empty signal to the server. You can keep the server script as it is!

1 Like

Local Script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debounce = true

ReplicatedStorage:WaitForChild("SpinningParts"):Clone().Parent = workspace.ObbyMap["Stage 10Special"]
ReplicatedStorage:WaitForChild("SpinnerS6"):Clone().Parent = workspace.ObbyMap["Stage 6Special"]

function onTouched(part)
    local check = part:IsDescendantOf(Players.LocalPlayer.Character)
    local player = Players:GetPlayerFromCharacter(part.Parent)

    if check and debounce then
        debounce = not debounce
        print(player.Name)

        ReplicatedStorage:WaitForChild("DamagePlayerOnS6"):FireServer(player)
        debounce = not debounce
    end
end

workspace.ObbyMap["Stage 6Special"].SpinnerS6.Lava.Touched:Connect(onTouched)
workspace.ObbyMap["Stage 6Special"].SpinnerS6.Lava1.Touched:Connect(onTouched)	

Server Script:

game.ReplicatedStorage:WaitForChild("DamagePlayerOnS6").OnServerEvent:Connect(function(player)
    player.Character.Humanoid:TakeDamage(5000)
    print(player.Name)
end)

now obviously this code can be improved, variables should be used to clean code

anyways it should work now

1 Like