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

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

Asked by
danglt 185
5 years ago

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)
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). User#25115 0 — 5y
0
I also recommend wrapping your data store request in a pcall. User#25115 0 — 5y
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 Vulkarin 581 — 5y

1 answer

Log in to vote
2
Answered by
Thetacah 712 Moderation Voter
5 years ago
Edited 5 years ago

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

0
Ah you beat me to it, good job Vulkarin 581 — 5y
0
use [test](url) for links User#5423 17 — 5y
0
Thanks, @kingdom5 Thetacah 712 — 5y
Ad

Answer this question