Button Respawn Players

So recently i have been encountering this issue with my admin panel. I tried make a button that respawn all players using remote event. Here is the script

Button Script:

local characterLoader = game.ReplicatedStorage:WaitForChild("RemoteEvent")

script.Parent.MouseButton1Click:Connect(function()
	characterLoader:FireServer() --this makes the player load their character
end)

Server Script:

local re = game.ReplicatedStorage.RemoteEvent
local player = game:GetService("Players")

re.OnServerEvent:Connect(function()
	game.Players.PlayerAdded:Connect(function(plr)
		plr.Character.Humanoid.Health = 0
		player.RespawnTime = 0
	end)
end)
2 Likes

OnServerEvent has a player parameter so do it this way.

Server Script

local re = game.ReplicatedStorage.RemoteEvent

re.OnServerEvent:Connect(function(player)
		player.Character.Humanoid.Health = 0
		player.RespawnTime = 0
end)

Button Script

local characterLoader = game.ReplicatedStorage:WaitForChild("RemoteEvent")

script.Parent.MouseButton1Click:Connect(function()
	characterLoader:FireServer() --this makes the player load their character
end)

EDIT: player doesn’t have a RepawnTime property

1 Like

Instead of killing the player by setting their health to 0 you can use the function :LoadCharacter on the player. Its only accessible from the server if I’m right.

1 Like

I think so. I’d have to recheck but go ahead and try.

1 Like

I did recheck and you are right

1 Like

Put this in the server script:

local re = game.ReplicatedStorage.RemoteEvent
local player = game:GetService("Players")

re.OnServerEvent:Connect(function()
  for i, plr in pairs(game.Players:GetChildren()) do
    plr:LoadCharacter()
  end
end)

or if you want to only respawn your player then do this instead
In button script:

local characterLoader = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	characterLoader:FireServer(player) --this makes the player load their character
end)

and in server script:

local re = game.ReplicatedStorage.RemoteEvent
local player = game:GetService("Players")

re.OnServerEvent:Connect(function(plr)
	plr:LoadCharacter()
end)

Just to add on, its a good habit adding server checks to limit exploiters where needed. Such as adding a magnitude check on the click detector from the server’s view.

1 Like

thanks man you really saved me