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 variable get ''deleted'' on death? [SOLVED]

if not player:FindFirstChild('Level') then
	level = Instance.new('NumberValue', player)
		level.Name = 'Level'
		level.Value = 0
end

onlevelupetc:connect(function()
player.Character.Humanoid.MaxHealth = level.Value * 40 + 350
end)

As I had previously stored Level in Character i tried storing it in Player so it wouldn't reset on death.

When I die the first time, everything works fine with the level value carrying over to the other life. However, if I level up on that second life, it says that variable level is nil on Line 8, even though it refers to the Level NumberValue which is still there. Im not sure how this could happen, could anyone help me figure it out?

SOLVE EDIT: Just needed to add an else to it to redefine it, all good

4 comments

0

Have you tried using this maybe? http://wiki.roblox.com/index.php?title=API:Class/Instance/WaitForChild otherwise, can you please post the output instead of a summary of the output?

0

Players.Player1.PlayerGui.dec:155: attempt to index global 'level' (a nil value) ------- And WaitForChild would endlessly wait until the value is made which will never happen because its yielding the thing that makes the value.

0

What about the hierchy of the objects? If the value is in any pack in the player, it will be deeted on death, also, if it s in the player it will be auto-deleted on death. try testing it on solo mode in studio so you can check to see if the value is there or not after death.

0

The player object is just an authentication that you're in the game. Nothing in it is deleted on death. (although things like backpcack are cleared)

1 answer

In your snippet, you don't define level in the event that it does exist.

You can try something like this:

local level = player:FindFirstChild("Level") -- Hope that it exists

if not level then
	-- It didn't, so I need to make a new one
	level = Instance.new("IntValue", player)
	level.Name = "Level"
	level.Value = 0
end

Another option would be to use :WaitForChild, and make another script responsible for adding all stats to the player exactly once, when they join.

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