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

Why can't it tell the value is there when it clearly is?

Asked by
tumadrina 179
9 years ago
game.Players.PlayerAdded:connect(function(Player)
    local leaderstats=Instance.new("StringValue")
    leaderstats.Parent=Player
    leaderstats.Name="leaderstats"
    local lvl=Instance.new("IntValue")
    lvl.Parent=leaderstats
    lvl.Parent=leaderstats
    lvl.Name="Level"
    repeat wait() until
    leaderstats
end)
----
game.Players.PlayerAdded:connect(function(Player)
    Player.leaderstats.Level.Changed:connect(function()--line 14
        local Debris=game:GetService("Debris")
        local Light=Instance.new("PointLight")
        Light.Parent=Player.Character.Torso
        Light.Brightness=math.huge
        Light.Color=Color3.new(255,225,0)
        Debris:AddItem(Light,5)
    end)
end)

the error is leaderstats is not a valid member of Player on line 14. On ROBLOX studio, you can see the value, but I get this error in both studio and in a server.

0
What is the problem? Describe it with words. What do you think is wrong? What testing have you done? Is there an error? BlueTaslem 18071 — 9y
0
oh sorry right, I'm out of it right now... tumadrina 179 — 9y

1 answer

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

First: why are you connecting two things to the same event?

That's bound to be confusing. In this case, it's likely the cause of an error, since if the second one acts first, there won't be a leaderstats objects (when it acts -- though there will be immediately after, as you are observing).


To fix that, we can write the second one using Player:WaitForChild("leaderstats"):WaitForChild("Level").Changed.


Or we can just use one event.

Here are other issues you should address:

  • Use second parameter of Instance.new to shorten things up
  • Use whitespace -- no reason to bunch your code up and make it unreadable
  • No need to parent leaderstats twice
  • Pointless repeat until loop since the condition will always pass since you just made the leaderstats object (and nothing can change that in the middle since it's local)
  • Use a lowercase player if you're going to use other lowercase variable names.
  • Don't use math.huge ever.
game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("StringValue", player)
    leaderstats.Name = "leaderstats"
    local lvl = Instance.new("IntValue", leaderstats)
    lvl.Name = "Level"

    lvl.Changed:connect(function()
        local light = Instance.new("PointLight", player.Character.Torso)
        light.Brightness = 60
        light.Color = Color3.new(255,225,0)
        local Debris = game:GetService("Debris")
        Debris:AddItem(light, 5)
    end)
end)

You should be careful of lvl changing while the Character doesn't have a torso though.

The following would be safer:

if player.Character and player.Character:FindFirstChild("Torso") then
    local light = Instance.new ......
0
Why never use math.huge? It must be useful in some situation. Perci1 4988 — 9y
0
If you actually NEED "bigger than any other number" go ahead and use it. That situation is extremely uncommon. In this case, the fact that it produced any light at all, consistently, when given `math.huge` should be a SURPRISE. It's not at all an acceptable use. BlueTaslem 18071 — 9y
Ad

Answer this question