How to enable a script after dying?

if I print inside of the while loop it stops printing after I revieve. Wouldn’t that mean that the health does change.

Yes, but if the Script doesn’t run ( The Sprint ) then maybe it’s an issue of that script and not exactly this one itself.

Wait, does the script run before dying?

ok you are actually correct. For some reason it prints in 2 sets so I couldn’t see it without scrolling lol.
Although the health is 100 on both the client and server, so not sure what to do.

The issue could be that the script was enabled before dying, therefore Enabling it again wouldn’t do anything. Try this:

local Character = script.Parent

local Humanoid = Character:WaitForChild("Humanoid")
local Sprint =  Character:WaitForChild("Sprint")

Humanoid.Died:Connect(function()
    Sprint.Enabled = false

	while Humanoid.Health == 0 do
		task.wait()
	end

	Sprint.Enabled = true
end)

your version doesn’t seem to work, however this worked once. Very weird. It doesn’t work anymore when I test it .-.

local player = game.Players.LocalPlayer
local char = player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
	local player = game.Players.LocalPlayer
	local char = player.CharacterAdded:Wait()
	local Humanoid = char:WaitForChild("Humanoid")

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

Was the script in StarterCharacterScripts?

yes your script was placed in StarterCharacterScripts.
However the new one I wrote works when in StarterPlayerScripts, but only works when the play has died once. If you then if you reset again it doesn’t work. The while loop also doesn’t seem print anymore.

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

local function characteradded(char)
	local Humanoid = char:WaitForChild("Humanoid")
	
	Humanoid.Died:Connect(function()
		while Humanoid.Health == 0 do
			task.wait()
			print(Humanoid.Health)
		end
		char.Sprint.Enabled = true
		print(char.Sprint.Enabled)
	end)
end)

characteradded(char)
player.CharacterAdded:Connect(characteradded)

Expected identifier when parsing expression, got ‘)’
(line 17)

Edited, try the solution again.

same error. Not sure why it’s unsatisfied with the parsing

I hate to just jump in here with my own view on this, I read through all the posts and it seems you two are making progress, however, I did want to just point out a few things…

  1. Yes there is an Enable on scripts
  2. If the server can find a local script, such as it can look into the Character, and find it, then it CAN enable it.

Also, if you put a script in StarterCharacterScripts, the script will be recreated each time the character loads, no need to do CharacterAdded

When in the StarterCharacterScrips …
image
A script will not run, before the Character is created.
So game.players.LocalPlayer.Character will NOT return nil

1 Like

well the one that is closest to working is in StarterPlayerScripts. The one that works when dying once.

Also there is this error if you print it idk why.
image

So, your original post, you want there to be a Sprint script added after checking data stores, I suppose to see if they purchased or own it, etc…

To do this, I would have the server check the data stores, and when it checks, it adds an attribute to the player (which will not get erased when the character resets)
After checking the data store, do Player:SetAttribute(“OwnsSprint”,true) if the data store says they own it.
Also on the server, have a CharacterAdded event fire (will fire each time the character resets), and the CharacterAdded can see if Player:GetAttribute(“OwnsSprint”) == true, and if so, it can take a sprint script that is a localscript from ServerStorage, and parent a copy of that script to the Character to give them sprint ability.

1 Like

I have to disable the sprint script because the sprint script will not work properly if the data hasn’t been loaded.
So you suggest that I replace the remotevent’s code to add an attribute instead of enabling the script? And then I do a while do loop inside the sprint script that has the attribute as a condition to mimic the disabling that is done manually in studio?
image

I am suggesting to only put the sprint script into the character if they own it.
That way, someone can’t just cheat, and turn on the sprint script by enabling it, because it wont be there, unless the server puts it there.

Something like this…

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if player:GetAttribute("OwnsSprint") == true then
			local sprintScript = game.ServerStorage:WaitForChild("SprintScript"):Clone()
			sprintScript.Parent = character
		end	
	end)
	--check data store
	--if we own sprint
	player:SetAttribute("OwnsSprint",true)
	
end)
1 Like

like this?

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		if plr:GetAttribute("OwnsSprint") == true then
			local sprintScript = game.ServerStorage:WaitForChild("SprintScript"):Clone()
			sprintScript.Parent = character
		end	
	end)
	
	local stamDataStore = Datastore2("stamMax",plr)
	
	local valholder = Instance.new("Folder", plr)
	valholder.Name = "valHolder"
	
	local stamMax = Instance.new("NumberValue")
	stamMax.Parent = plr.valHolder
	stamMax.Name = "stamMax"
	
	local function maxUpdate(updatedValue)
		stamMax.Value = stamDataStore:Get(updatedValue)
	end
	
	maxUpdate(defaultValue)
	stamDataStore:OnUpdate(maxUpdate)
	
	local stamCurrent = Instance.new("NumberValue")
	stamCurrent.Parent = plr.valHolder
	stamCurrent.Name = "stamCurrent"
	stamCurrent.Value = stamMax.Value
	
	plr:SetAttribute("OwnsSprint",true)
	
	--enable:FireClient(plr)

end)


ok it doesn’t work before resetting of course

alright final version. Just had to do it once when the player joins. Good idea to use a more useful value insetad of .Enabled/.Disabled @SelDraken

game.Players.PlayerAdded:Connect(function(plr)
	
	local stamDataStore = Datastore2("stamMax",plr)
	
	local valholder = Instance.new("Folder", plr)
	valholder.Name = "valHolder"
	
	local stamMax = Instance.new("NumberValue")
	stamMax.Parent = plr.valHolder
	stamMax.Name = "stamMax"
	
	local function maxUpdate(updatedValue)
		stamMax.Value = stamDataStore:Get(updatedValue)
	end
	
	maxUpdate(defaultValue)
	stamDataStore:OnUpdate(maxUpdate)
	
	local stamCurrent = Instance.new("NumberValue")
	stamCurrent.Parent = plr.valHolder
	stamCurrent.Name = "stamCurrent"
	stamCurrent.Value = stamMax.Value
	
	plr:SetAttribute("OwnsSprint",true)
	
	if plr:GetAttribute("OwnsSprint") == true then
		local sprintScript = game.ServerStorage:WaitForChild("Sprint"):Clone()
		sprintScript.Parent = plr.Character
	end

	plr.CharacterAdded:Connect(function(character)
		if plr:GetAttribute("OwnsSprint") == true then
			local sprintScript = game.ServerStorage:WaitForChild("Sprint"):Clone()
			sprintScript.Parent = character
		end	
	end)
end)