Help with Currency

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Whenever I reach 30 “Coins”, a part will change color

  2. What is the issue? Part doesn’t change color

  3. What solutions have you tried so far? Tried putting the Part color changing section into a separate script

So I want to make it whenever I reach 30 Coins, Part would change color

Part is called ColoredPart if it helps


game.Players.PlayerAdded:Connect(function(player)
	local Coins = Instance.new("IntValue", player)
	Coins.Name = "Coins"
	Coins.Parent = player
	
	while wait(2) do 
		Coins.Value = Coins.Value + 5
	end
	
	if Coins.Value >= 30 then
		game.Workspace.ColoredPart.BrickColor = BrickColor.new(0, 255, 0)
	end
end)

Anyone might be able to figure out why? I don’t get any errors in output either.

4 Likes

I’m think you cant use BrickColor.new(0, 255, 0).
You only can use BrickColor.new(“Green”)
Or game.Workspace.ColoredPart.Color3 = Color3.new(0, 255, 0) – and you cant use 0, 255, 0. Use 0.0666667, 1, 0

1 Like

Your while loop isn’t ending. If you want to escape it, you need to have a condition instead of wait(2).

while Coins.Value < 30 do -- placeholder condition
	Coins.Value = Coins.Value + 5
	wait(2) -- the wait has been moved here
end

Edit: The post above is also correct. Either you use Color3.fromRGB(0, 255, 0) for what you currently have, or just use BrickColor.new("Green")

1 Like

Firstly you need to use ColoredPart.Color3 = Color3.fromRGB(0, 255, 0)
Secondly you need to wrap the loop in a coroutine like this:

coroutine.resume(coroutine.create(function()
	while true do
		task.wait(2)
		Coins.Value += 5
	end
end))

Edit: You need to wrap it in a coroutine so that the code after the loop runs
Another issue with the code is that it only checks if you have more than 30 coins once, instead you need to check it every time it updates:

Coins:GetPropertyChangedSignal("Value"):Connect(function()
	if Coins.Value >= 30 then
		game.Workspace.ColoredPart.Color3 = Color3.fromRGB(0, 255, 0)
	end
end)
2 Likes

hello, try using Changed. Example:

Coins.Changed:connect(function()

	if Coins.Value >= 30 then
		game.Workspace.ColoredPart.Color3 = Color3.fromRGB(0, 255, 0)
	end
end)
1 Like

I’m forget you can use ColoredPart.Color3 = Color3.fromRGB(0, 255, 0)

1 Like

Thanks, this solved my problem. I didn’t realize it was constantly looping.

2 Likes