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)
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)
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.
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.