Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

This script stops working after your character dies, can someone help?

After your roblox character dies, the script stops working. Could someone fix it?

local plr = game:GetService'Players'.LocalPlayer
local human = plr.Character.Humanoid


--------SETTINGS--------
local settings = {
	false, --autoclick(only use if you have a mower that needs clicking to farm)
	false --warns the position you are going to in console, not recommended for afk-farming.
}
--------SETTINGS--------

while wait() do
	for _,v in next, workspace.GRASS:GetChildren() do
		human.WalkToPoint = v.Position
		if settings[1] == true then
			mouse1press()
		end
		if settings[2] == true then
			warn(v.Position)
		end
		wait()
	end
end

1 answer

The player's character is not always guaranteed to exist, which is why you need to set up a bunch of things in order to make sure you always have the character.

First off, you need to make sure that the character exists when you're first getting it. If it doesn't exist, then wait until it is added to the workspace.

--LocalScript in whatever will be picked up by the client i guess idk
local PLAYERS = game:GetService("Players")
local Player = PLAYERS.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() --will check first if Player.Character exists; if not, wait for it to be added to workspace and then get it

However, we face an issue when the character dies. The character is destroyed when it dies and is replaced with a new character, so we need to update the Character variable when the character is added back to workspace.

--LocalScript in whatever will be picked up by the client i guess idk
Player.CharacterAdded:Connect(function(character)
	Character = character
end)

Now you should see that the character updates every time it is respawned. You also have to make sure that the character exists whenever you reference it, or things will go wrong. Here's an example script that sets the character's walkspeed to a random number every time it's respawned.

--LocalScript in StarterPlayerScripts
local PLAYERS = game:GetService("Players")
local Player = PLAYERS.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() --will check first if Player.Character exists; if not, wait for it to be added to workspace and then get it
local Humanoid = Character:WaitForChild("Humanoid")

Player.CharacterAdded:Connect(function(character)
	Character = character
	Humanoid = Character:WaitForChild("Humanoid")
	Humanoid.WalkSpeed = math.random() * 64
end)

If you want a quick and dirty way of doing it, though, just make a LocalScript parented to StarterCharacterScripts, which should update every time the character is respawned.

--LocalScript in StarterCharacterScripts
local Character = script.Parent --the script is parented to the character so we can easily get it
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.WalkSpeed = math.random() * 64

I prefer this method because it doesn't involve RBXScriptSignals, which are a bit dirty, but many prefer the former method, so do what you want.

This discussion is preserved as it was. New answers, comments, and votes are closed.