Why my script stop working if player die

  1. What do you want to achieve? I’m making a script that make the player move right if he hold R

  2. What is the issue? The script stop working if player die

  3. What solutions have you tried so far? I tried to look at the script and see if there is any issue but I don’t notice any thing and I tried to disable the script and enable it again if player die but its still stop working when player die

This is the script (Its a local script and its in StarterPlayerScripts)

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

local humanoid = Character:WaitForChild("Humanoid")
local Moving = false
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

UserInputService.InputBegan:Connect(function(keyCode, Chatted)

	if Chatted then
		return
	end

	local HRP = Character:FindFirstChild("HumanoidRootPart")

	if HRP ~= nil and keyCode.KeyCode == Enum.KeyCode.R then
		Moving = true

		while Moving do
			humanoid:Move(Vector3.new(2,0,2))
			RunService.RenderStepped:Wait()
		end

	end
end)

UserInputService.InputEnded:Connect(function(keyCode)
	local HRP = Character:FindFirstChild("HumanoidRootPart")

	if HRP ~= nil and keyCode.KeyCode == Enum.KeyCode.R then
		Moving = false
		Character:MoveTo(HRP.Position)
	end

end)
1 Like

When the player dies, their previous character is removed.

This becomes nil because the Humanoid has been removed from the game.

Same thing with this ^


Try using StarterCharacterScripts, the script will be recreated every time the player dies and respawns.

5 Likes

Ty bro

(Extra Characters aaaaa)

1 Like

Who knew it was this simple to fix this. Thank you my guy, you saved me.