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

OnPlayerAdded not adding a Instance.new BrickColorValue to the player?

This script is located in ServerScriptService but doesn't add the Value although adds everything else

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("Tiles")



game.Players.PlayerAdded:Connect(function(player)
	local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"
	
	local tiles = Instance.new("IntValue",leader)
 tiles.Name = "TilesColored"
 tiles.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, tiles.Value)
	
	local ye = Instance.new("BrickColorValue",player)
	ye.Name = "colorval"
	ye.Value = math.random()
end)

3 comments

0

You should not use the second argument of the Instance.new constructor. It is deprecated and bad practice (it was deprecated because it was bad practice).

0

I also recommend wrapping your data store request in a pcall.

0

Instance.new() second argument wouldn't matter in this case, none of these Instances are being rendered so changing their properties afterwards isn't making a difference. It's possible that one of your datastore lines is hanging, you should add prints before and afterwards. You can't assign math.random() as a brickcolor, you can do BrickColor.Random() instead

1 answer

✓ Accepted answer

Good afternoon!

math.random() returns a random number. BrickColorValue's Value property is of type BrickColor, not number.

I believe you're looking for BrickColor.Random()

Line 17 should be:

ye.Value = BrickColor.Random()

As far as the BrickColorValue not appearing in the player, I've put your code in studio and it seems to function as intended. Be sure that your script is NOT a LocalScript and is inserted in ServerScriptService.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("Tiles")



game.Players.PlayerAdded:Connect(function(player)
    local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"

    local tiles = Instance.new("IntValue",leader)
 tiles.Name = "TilesColored"
 tiles.Value = ds:GetAsync(player.UserId) or 0
 ds:SetAsync(player.UserId, tiles.Value)

    local ye = Instance.new("BrickColorValue",player)
    ye.Name = "colorval"
    ye.Value = BrickColor.Random()
end)

In the future, you should also refrain from parenting new objects before setting their properties. You can read about why here

3 comments

0

Ah you beat me to it, good job

0

use test for links

0

Thanks, @kingdom5

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