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

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

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

✓ Accepted answer

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.

1 comment

0

Thanks!

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