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

What is wrong with this script that creates a NumberValue in the player?

Asked by 9 years ago

This script stores a data value in the player when they join, and updates it depending on the the value stored in a DataStore.

Output: NumberValue is not a valid member of player

local DataStore = game:GetService("DataStoreService"):GetDataStore("Level")
game.Players.PlayerAdded:connect(function(player)
    local key = "user_"..player.userId
    Instance.new("NumberValue",game.Players:FindFirstChild(player))

    if DataStore:GetAsync(key) == nil then
        game.Players:FindFirstChild(player).NumberValue.Value = 0
        DataStore:SetAsync(key,0)
    else
        game.Players:FindFirstChild(player).NumberValue.Value = DataStore:GetAsync(key)
        print("Got here") 
    end

end)

Thanks, MD

1 answer

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

The default name of the "___Value" (e.g., NumberValue, CFrameValue, ObjectValue) objects is just "Value", rather than their classname (NumberValue in this case).

In either case, it would be much better to give the object a descriptive name, doing something like:

Instance.new("NumberValue", player).Name = "SomeName";

(And then referring to it by .SomeName.


What might be cleaner in your script (in addition to giving it a clear name) would be to refer to it as a variable:

local myValue = Instance.new("NumberValue", player);
myValue.Name = "SomeValue";

...

myValue.Value = ...;

Note that player is the reference to the player. You do not need to use game.Players:FindFirstChild(player). Just player is exactly the same value.

0
Thanks! MasterDaniel 320 — 9y
Ad

Answer this question