Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago
Edited by Azarth 5 years ago

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

01local plr = game:GetService'Players'.LocalPlayer
02local human = plr.Character.Humanoid
03 
04 
05--------SETTINGS--------
06local settings = {
07    false, --autoclick(only use if you have a mower that needs clicking to farm)
08    false --warns the position you are going to in console, not recommended for afk-farming.
09}
10--------SETTINGS--------
11 
12while wait() do
13    for _,v in next, workspace.GRASS:GetChildren() do
14        human.WalkToPoint = v.Position
15        if settings[1] == true then
View all 23 lines...

1 answer

Log in to vote
1
Answered by
y3_th 176
5 years ago

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.

1--LocalScript in whatever will be picked up by the client i guess idk
2local PLAYERS = game:GetService("Players")
3local Player = PLAYERS.LocalPlayer
4local 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.

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

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.

01--LocalScript in StarterPlayerScripts
02local PLAYERS = game:GetService("Players")
03local Player = PLAYERS.LocalPlayer
04local 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
05local Humanoid = Character:WaitForChild("Humanoid")
06 
07Player.CharacterAdded:Connect(function(character)
08    Character = character
09    Humanoid = Character:WaitForChild("Humanoid")
10    Humanoid.WalkSpeed = math.random() * 64
11end)

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.

1--LocalScript in StarterCharacterScripts
2local Character = script.Parent --the script is parented to the character so we can easily get it
3local Humanoid = Character:WaitForChild("Humanoid")
4Humanoid.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.

Ad

Answer this question