local p = game.Players:FindFirstChild(name.Text) local c = p.Character local h = c.Humanoid local res = game.ReplicatedStorage.Respawn.MainScript res.Parent = game.Workspace h.Health = 0 wait(0.001) res.Parent = game.ReplicatedStorage
GENERAL PRACTICE
Use workspace
instead of game.Workspace
Don't use :FindFirstChild()
to get a player from Players since if an object with the same name as a player was added to the service, it may be picked instead.
Use :WaitForChild()
to make sure a part exists before changing / using it
ISSUES
You can use the :LoadCharacter()
function of a player to make them respawn without the need to die, however this must be done in a Server Script.
Since you are using a LocalScript, you will require a RemoteEvent
NOTES
Your example script doesn't list the local "name" variable so you will have to add this, along with its functional connection to the script.
REVISED LocalScript (Change your above code to this, keeping your connection / name variable)
local remote = game:GetService("ReplicatedStorage"):WaitForChild("RespawnPlayer") local res = game:GetService("ReplicatedStorage"):WaitForChild("Respawn"):WaitForChild("MainScript") res.Parent = workspace remote:FireServer(name.Text) wait(0.001) res.Parent = game:GetService("ReplicatedStorage")
REVISED Server Script (Add this under ServerScriptService)
local remote = Instance.new("RemoteEvent") remote.Name = "RespawnPlayer" remote.Parent = game:GetService("ReplicatedStorage") remote.OnServerEvent:Connect(function(p) p:LoadCharacter() end)
Use :LoadCharacter()
local Player = game.Players:FindFirstChild(name.Text) --Before this line you should add an event like Button.MouseButton1Click Player:LoadCharacter()
Hello! If you want to respawn a player instantly use :LoadCharacter() instead of Humanoid.Health = 0. Here is an example:
wait(10) game.Players.JakyeRU:LoadCharacter() -- My character will be respawned after 10 seconds.
EDIT: You can only use the :LoadCharacter() from a Server Script. If you want to use it from a LocalScript then you have to use RemoteEvents.