It should be Button1.Value.Value otherwise you are referencing the instance, not the value inside it.
The reason why it is not changing color is because you have placed the if coins.Value < 250 statement inside the other if statement. You need to move it and indent to the left to take it out of the other if statement.
You also have your button1 touched function inside your playeradded function.
I have rewritten the script to fix the problems:
local Button1 = workspace.Button1
Button1.Value.Value = 250
game.Players.PlayerAdded:Connect(function(joining) --When the player joins the game…
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = joining
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
end)
Button1.Touched:Connect(function(touching)
local humanoid = touching.Parent:FindFirstChild("Humanoid")
if humanoid then
local player = game.Players:GetPlayerFromCharacter(touching.Parent)
local Coins = player.leaderstats.Coins
if Coins.Value >= 250 then
Coins.Value = Coins.Value - Button1.Value.Value
else
Button1.BrickColor = BrickColor.new("Really red")
end
end
end)
A summary of my changes:
Button1’s Value is only assigned once, that beaing at the start of the script and not every time a player joins
The touching function is separate to the playeradded function
We get the player of the humanoid so we can access leaderstats
We use else to cover any cases that are not selected by if.
This script has 2 functions: PlayerAdded and Touched.
When we define a variable within a function, we say that this variable is in the scope of the function.
Since we define the coins value within the first function, it is out of the scope of the second function. This means that the variable cannot be used in the other function, so it needs to be redefined within the scope of that function.
game.Players:GetPlayerFromCharacter(Character) gets the player from the character. What this means:
A ‘player’ is made up of two components: A player, found in players, and a character, found in workspace. The character is the avatar you see when playing or testing a game. The player holds data about the player and the character. Your Coins value in leaderstats is in your Player, but you will touch the button1 with your character. For this reason, we need to get the player of the character, which is what this code does.