How to enable a script after dying?

One of my scripts “Sprint” is disabled until the server finds the data in datastore. The issue is that it of course only does this when the player has joined the game. So when the humanoid dies, the Sprint script is disabled again, but not enabled.

I tried to write a script below, but it doesn’t work. I think it’s because the script itself is removed and readded to the player when Health > 0, so the script just goes back to the first line and doesn’t trigger what comes after the while loop :confused:

local Humanoid = game.Players.LocalPlayer.Character.Humanoid

Humanoid.Died:Connect(function()
	while Humanoid.Health == 0 do
		task.wait()
	end
	game.Players.LocalPlayer.Character.Sprint.Enabled = true
	print(game.Players.LocalPlayer.Character.Sprint.Enabled)
end)
10 Likes

Script.Enabled doesn’t exist. It’s “disabled”

Code:

local Humanoid = game.Players.LocalPlayer.Character.Humanoid

Humanoid.Died:Connect(function()
	while Humanoid.Health == 0 do
		task.wait()
	end
	game.Players.LocalPlayer.Character.Sprint.Disabled= false
	print(game.Players.LocalPlayer.Character.Sprint.Disabled)
end)
4 Likes

image
you can use both .Disabled and .Enabled
it doesn’t change anything other than what the boolValue has to be

2 Likes

You can’t enable or disable a script in local (if the script that is to disable isn’t localscript).

2 Likes

Sprint is a localscript. In fact I used a RemoteEvent in my serverscript because I know this.

Try to read my hypothesis in my post about why it doesn’t work.

1 Like

Chill you don’t need to be that harsh

Try to activate it in server. You can make another Remote and do this:

Client

local Humanoid = game.Players.LocalPlayer.Character.Humanoid

Humanoid.Died:Connect(function()
	while Humanoid.Health == 0 do
		task.wait()
	end
	remoteEvent:FireServer(game.Players.LocalPlayer.Character.Sprint)
	print(game.Players.LocalPlayer.Character.Sprint.Disabled)
end)

Server

RemoteEvent.OnServerEvent:Connect(function(Script)
Script.Disabled = false
end)

Idk if this one works. But try to activate it into server from a RemoteEvent instead

2 Likes

The Sprint script is local so enabling it on the server wouldn’t work in the first place,
but like I said in my post: The script can’t go to code past the while do loop. Local/Server has nothing to do with anything if everything is done locally, no?

2 Likes

Are you putting that script in StarterCharacterScripts? You should put the script in StarterPlayerScripts instead, where it doesn’t get re-added every time.

3 Likes

yep I tried that earlier, and it can’t find the Character at all if it’s not in StarterCharacterScripts
maybe because it’s a customCharacter?

print(game.Players.LocalPlayer.Character)
output: nil

Maybe I am writing the destination incorrectly?

1 Like

Because at the time, the character has not loaded yet. So the script finds it as nil.
Player has an event called Player.CharacterAdded(), which fires whenever the character spawns. You should modify your script a bit with it.

Input: local Humanoid = game.Players.LocalPlayer.CharacterAdded().Humanoid
Output: Players.youbigown.PlayerScripts.diedSprintEnable:2: attempt to call a RBXScriptSignal value

Whoops, don’t include the parenthesis. It’s an event.

1 Like

Humanoid is not a valid member of RBXScriptSignal

ok I found a fix for that, but then the script still doesn’t trigger what comes after the while do loop…
image
it should be in here right?

1 Like

Are you trying to do it after dying or before dying?

after revieving. The function is trying to emulate the death process.

So another script will set the health of it reviving the humanoid?

no. It’s like this: The humanoid dies from the player resetting or getting killed by something.
Then the event is triggered and the function is run. Inside the function is a loop that waits for the player to be revived theoretically. The problem is that in practice the loop does run, but when it’s done (humanoid has been revived) the script just ignores everything after the while loop. In this case it ignores game.Players.LocalPlayer.Character.Sprint.Enabled = true print(game.Players.LocalPlayer.Character.Sprint.Enabled)

Alright try to do this: add another debug print: print(Humanoid.Health), after that print Sprint, not Sprint.Enabled.
Show me what it says after.

Anything after the while loop won’t be run, so the print can’t be there.
I put it here

Humanoid.Died:Connect(function()
	print(Humanoid.Health)
	print(game.Players.LocalPlayer.Character.Sprint)
	while Humanoid.Health == 0 do
		task.wait()
	end
	game.Players.LocalPlayer.Character.Sprint.Enabled = true
	print(game.Players.LocalPlayer.Character.Sprint.Enabled)
end)

image

Set the Health of the humanoid to something more than zero on the server, see if the Script enables.
Maybe the reason why it doesn’t work is because it never changed the health so it will continue yielding waiting for something that will never happen.-

1 Like