Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this variable get ''deleted'' on death? [SOLVED]

Asked by
drew1017 330 Moderation Voter
8 years ago
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

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? rollercoaster57 65 — 8y
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. drew1017 330 — 8y
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. rollercoaster57 65 — 8y
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) drew1017 330 — 8y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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.

Ad

Answer this question