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

Why does this script only run once after the player dies?

So I'm trying to make a script where if the Current value is at 100 then the player dies, the script at the bottom is what I'm currently using to do it but the player only dies once and it never kills the player after that.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:connect(function(Char)
	local level = Instance.new("IntValue", player)
	level.Name = "Level"
	level.Value = 1
	
	local exp = Instance.new("IntValue", level)
	exp.Name = "Current"
	exp.Value = 0
	
	local maxExp = Instance.new("IntValue", level)
	maxExp.Name = "Max"
	maxExp.Value = 100
	
	local truevalue = Instance.new("BoolValue", player)
	truevalue.Name = "TrueValue"
	truevalue.Value = false
	
	exp.Changed:Connect(function(val)
		if exp.Value >= maxExp.Value then
				Char.Humanoid.Health = 0
				player.Level.Current.Value = 0
			end
		end)
	end)
end)

2 answers

I am not sure if this works can you try it?

local player = game.Players.LocalPlayer

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:connect(function(Char)
		local leaderstats = Instance.new("Folder", player)
		leaderstats.Name = "leaderstats"
		
		local level = Instance.new("IntValue", leaderstats)
		level.Name = "Level"
		level.Value = 1

		local exp = Instance.new("IntValue", leaderstats)
		exp.Name = "Current"
		exp.Value = 0

		local maxExp = Instance.new("IntValue", leaderstats)
		maxExp.Name = "Max"
		maxExp.Value = 100

		local truevalue = Instance.new("BoolValue", leaderstats)
		truevalue.Name = "TrueValue"
		truevalue.Value = false

		exp.Changed:Connect(function(val)
			if exp.Value >= maxExp.Value then
				Char.Humanoid.Health = 0
				player.Level.Current.Value = 0
			end
		end)
	end)
end)

So the reason it doesn't work is because the player.CharacterAdded is under the Players.PlayerAdded, and a player only gets added once, which means Players.PlayerAdded won't fire again and because player.CharacterAdded only fires if Players.PlayerAdded fires, the script won't run again after a player dies. To fix this, separate them, like this:

Put this in a server script:

game.Players.PlayerAdded:Connect(function(player)
    local level = Instance.new("IntValue", player)
    level.Name = "Level"
    level.Value = 1

    local exp = Instance.new("IntValue", level)
    exp.Name = "Current"
    exp.Value = 0

    local maxExp = Instance.new("IntValue", level)
    maxExp.Name = "Max"
    maxExp.Value = 100

    local truevalue = Instance.new("BoolValue", player)
    truevalue.Name = "TrueValue"
    truevalue.Value = false
end)

Put this in a localscript inside StarterPlayerScripts:

local player = game.Players.LocalPlayer
local maxExp = player:WaitForChild("leaderstats").Max
local exp = player:WaitForChild("leaderstats").Current
local level = player:WaitForChild("leaderstats").Level

player.CharacterAdded:Connect(function(Char)
	exp.Changed:Connect(function()
        	if exp.Value >= maxExp.Value then
                	Char.Humanoid.Health = 0
                	player.Level.Current.Value = 0
           	 end
        end)
end)

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