When touching part, and not having enough coins, not changing colour


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

local Button1 = workspace.Button1
Button1.Value.Value = 250

Button1.Touched:Connect(function(touching)
	local humanoid = touching.Parent:FindFirstChild("Humanoid")
	
	if Coins.Value >= 250 and humanoid then
		Coins.Value = Coins.Value - Button1.Value
		
		if Coins.Value < 250 and humanoid then
			Button1.BrickColor = BrickColor.new("Really red")
		end
	end
end)

end)

2 Likes

There is an error on this line:

Coins.Value = Coins.Value - Button1.Value

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.

Hope this helps! :grinning:

3 Likes

HEY! Thanks for getting back! The colour changing one was the problem!

2 Likes

For some reason, despite me having 282 coins it isnt deducting it, its actually still turning in red

1 Like

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.

Please don’t hesitate if you have any questions.

1 Like

Thank you very much! 2 things though.
So, first of all im quite new so this is probably basic stuff

  1. Why is coins getting defined twice?
  2. What does gets the player do?
1 Like
  1. 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.

  2. 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.

1 Like

ohh ok thanks that makes sense!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.