Leaderstats not decreasing when i have enough coins, saying "i cant afford it"



script.Parent.MouseButton1Click:Connect(function(coins)
local plr = game.Players.LocalPlayer

local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")

coins.Value = coins.Value+1

end)


local Button1 = script.Parent
local Button1Value = 250

Button1.Touched:Connect(function(touched)
local plr = game.Players:GetPlayerFromCharacter(touched.Parent)
local Num1 = workspace.Num1
if plr and plr.leaderstats.Coins.Value >= 200 then
Button1:Destroy()

	Num1.Part.Transparency = 0
	print("You can afford it")


	local TotalScore = plr.leaderstats.Coins.Value - Button1Value
	print(TotalScore)
	plr.leaderstats.Coins.Value = TotalScore

else
	Button1.BrickColor = BrickColor.Random()
	print("You cannot afford that")
end

end)

change this:

local plr = game.Players:GetPlayerFromCharacter(touched.Parent)

to this:

local plr = game.Players:FindFirstChild(touched.Parent.Name)
1 Like


still doesnt work

can you show the elements of your player in explorer??

this?
image

of game.players, not workspace

image

show me the values of leaderstats

1 Like

It is because you are increasing the amount of coins on the client (on a localscript). You need to use a RemoteEvent or something similar to communicate from client to the server. Right now the server sees that you have 0 coins (even though it shows 400 on your client and your client only) and thus will not allow you to buy it.

3 Likes

It should be

coins.Value += 1

You are adding the coins locally when it should be server wide.

What i recommend doing is creating a remote event inside replicated storage and call it “Update”

Inside the local script put this

script.Parent.MouseButton1Click:Connect(function()
  local plr = game.Players.LocalPlayer
  local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value
  local update = game.ReplicatedStorage:WaitForChild('Update')
  update:FireServer(coins+1)
end)

Inside the leaderstats script add this below your player added.

game.ReplicatedStorage:WaitForChild('Update').OnServerEvent:Connect(function(player, Amount)
      plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value = Amount
end)
2 Likes

what you’re doing is that you’re updating the coins value on the client which is not rendered to the server so in order to render the new coin value u need to do that through network communication (remote events) so u send a remote from the client requesting to update the coins value and the server does that. thats it

What’s the difference? Stop pointing out things that don’t contribute to the question

1 Like

create a script into the button and put this code:

script.Parent.MouseButton1Click:Connect(function(coins)
local plr = game.Players.LocalPlayer
local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")

coins.Value = coins.Value+1

Hey, the plr comes up with a blue line

My mistake, Do this instead!

game.ReplicatedStorage:WaitForChild('Update').OnServerEvent:Connect(function(player, Amount)
      player:WaitForChild("leaderstats"):WaitForChild("Coins").Value = Amount
end)

Yay! Thank you so much, ive been having this problem for a while now!

1 Like

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