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

[SOLVED] How do I assign BoolValues nad NumberValues as Children of Players?

I've been scripting an RPG, and I thought that it might be an idea to store Player Stats in BoolValues and NumberValues inside the Player, and then edit them when the player Levels Up, or completed a Quest. The only problem is that the Values simply aren't being assigned. I'm not getting any Debug Errors, and the output doesn't tell me anything specific but "Gold is not a valid member of Player", "Gems is not a valid member of Player", "PlayerLevel is not a valid member of player", etc.

Here's my code:

--Player value Variables
Gold = Instance.new("NumberValue")
Gems = Instance.new("NumberValue")
PlayerLevel = Instance.new("NumberValue")
Exp = Instance.new("NumberValue")
HP = Instance.new("NumberValue")
Quest1 = Instance.new("BoolValue")
Quest2 = Instance.new("BoolValue")
Quest3 = Instance.new("BoolValue")
Quest4 = Instance.new("BoolValue")
Quest5 = Instance.new("BoolValue")
Player = game.Players.LocalPlayer
Gold.Value = 10
Gems.Value = 0
PlayerLevel.Value = 1

--Gui frame variables
ScreenGui = game.StarterGui.ScreenGui
ShopBG = ScreenGui.ShopBG
QuestsBG = ScreenGui.QuestsBG
StatsBG = ScreenGui.StatsBG
OptionsBG = ScreenGui.OptionsBG

--Start of functions
wait(3)


a = game.Players.LocalPlayer:GetChildren()

function assignValues() --assigns LocalPlayer all necessary values
repeat wait() until Player --waits until Player is loaded before assigning values
for i,v in pairs(a) do
	if v:IsA("BoolValue" or "NumberValue") then --makes sure that the Player doesn't have values assigned already
		Player.MaxHealth = Player.HP.Value
	else --if they don't have values, then it assigns them values
	
	Gold.Parent = Player
	Gold.Name = "Gold"
	Gems.Parent = Player
	Gems.Name = "Gems"
	PlayerLevel.Parent = Player
	PlayerLevel.Name = "PlayerLevel"
	Exp.Parent = Player
	Exp.Name = "Exp"
	HP.Parent = Player
	HP.Name = "HP"
	Quest1.Parent = Player
	Quest1.Name = "Quest1"
	Quest1.Value = false
	Quest2.Parent = Player
	Quest2.Name = "Quest2"
	Quest2.Value = false
	Quest3.Parent = Player
	Quest3.Name = "Quest3"
	Quest3.Value = false
	Quest4.Parent = Player
	Quest4.Name = "Quest4"
	Quest4.Value = false
	Quest5.Parent = Player
	Quest5.Name = "Quest5"
	Quest5.Value = false
	
end		
end
end


game.Players.PlayerAdded:connect(assignValues)


1 answer

✓ Accepted answer

When testing PlayerAdded events, DO NOT go on PlaySolo. PlayerAdded events DO NOT FIRE on play solo. Start a server instead.

Gold = Instance.new("NumberValue")
Gems = Instance.new("NumberValue")
PlayerLevel = Instance.new("NumberValue")
Exp = Instance.new("NumberValue")
HP = Instance.new("NumberValue")
Quest1 = Instance.new("BoolValue")
Quest2 = Instance.new("BoolValue")
Quest3 = Instance.new("BoolValue")
Quest4 = Instance.new("BoolValue")
Quest5 = Instance.new("BoolValue")

These are all values. But they are only one value. If you want to create a separate set of values for each player, you need to create them inside a PlayerAdded event.

Player = game.Players.LocalPlayer

game.Players.LocalPlayer only works inside a local script. You will have to use other methods to get the player. Since PlayerAdded events have the built in parameter that equals the player that just entered the game, you can use that.

Gold = 10
Gems = 0
PlayerLevel = 1

Here, you are reassigning the variables, not changing the value of the instances you just created. To fix this, just do Gold.Value = 10, and so on.

ScreenGui = game.StarterGui.ScreenGui
ShopBG = ScreenGui.ShopBG
QuestsBG = ScreenGui.QuestsBG
StatsBG = ScreenGui.StatsBG
OptionsBG = ScreenGui.OptionsBG

You never use these variables, but even if you did, it would not do anything until the player died. To edit GUIs instantly, you need to change them in a player's PlayerGui.

Now all we have to do is to edit the main function. The for loop and if statement are not needed, since if a new player joins the game they will obviously not have the values.

function assignValues(Player) 
     
	 Gold = Instance.new("NumberValue")
	Gems = Instance.new("NumberValue")
	PlayerLevel = Instance.new("NumberValue")
	Exp = Instance.new("NumberValue")
	HP = Instance.new("NumberValue")
	Quest1 = Instance.new("BoolValue")
	Quest2 = Instance.new("BoolValue")
	Quest3 = Instance.new("BoolValue")
	Quest4 = Instance.new("BoolValue")
	Quest5 = Instance.new("BoolValue")


    Player.MaxHealth = Player.HP.Value
    Gold.Parent = Player
    Gold.Name = "Gold"
    Gold.Value = 10
    Gems.Parent = Player
    Gems.Name = "Gems"
    Gems.Value = 0
    PlayerLevel.Parent = Player
    PlayerLevel.Name = "PlayerLevel"
    PlayerLevel.Value = 1
    Exp.Parent = Player
    Exp.Name = "Exp"
    HP.Parent = Player
    HP.Name = "HP"
    Quest1.Parent = Player
    Quest1.Name = "Quest1"
    Quest1.Value = false
    Quest2.Parent = Player
    Quest2.Name = "Quest2"
    Quest2.Value = false
    Quest3.Parent = Player
    Quest3.Name = "Quest3"
    Quest3.Value = false
    Quest4.Parent = Player
    Quest4.Name = "Quest4"
    Quest4.Value = false
    Quest5.Parent = Player
    Quest5.Name = "Quest5"
    Quest5.Value = false

end


game.Players.PlayerAdded:connect(assignValues)

This should fix your problems, but the values won't be visible to the player. To make it so the player can see the values, create an IntValue named 'leaderstats' and parent all your other values into that. Then, they will appear on the leaderboard.

Hope i helped!

4 comments

0

Sorry, I didn't realize I put the outdated script in. I changed it, it's now in a regular script, and I got rid of the part where I set the Gold and Gems variables to 10 and 0. I'll update it now.

0

Also, the reason I used a for loop and if statement was to make sure that values aren't assigned to ALL players when 1 player joins, only to the player who joined.

0

Thanks so much, it finally worked!

0

Np

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