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 4 years ago
Edited by Azarth 4 years ago

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

Log in to vote
1
Answered by
y3_th 176
4 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.

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

Ad

Answer this question