Sell script doesn't work

My script doesn’t work and I don’t know why , it doesn’t give me any errors

local Part = script.Parent
Part.Touched:Connect(function(HIT)
	local H = HIT.Parent:FindFirstChild("Humanoid")	
	if H then
		local player = game.Players:GetPlayerFromCharacter(HIT.Parent)	
if player then
	local leaderstats = player:WaitForChild("leaderstats")
	local Currency = leaderstats.Gems
	local Selling = leaderstats.Candy 
		if Selling.Value > 0 then
			Currency.Value = Currency.Value + Selling.Value *10
			Selling.Value = 0
		end
end
end
end)

2 Likes
  1. Is it a script and not a local script?
  2. Is the candy value replicated to the server?

Its a normal script

the candy value is in the leaderstats folder that is in the Player

Can I see the script that gives the player the candy?

script.Parent.MouseButton1Down:Connect(function()
	game.Players.LocalPlayer.leaderstats.Candy.Value = game.Players.LocalPlayer.leaderstats.Candy.Value + 1
end)

its placed in an gui button

That’s what I thought it’s not replicated to the server. Give me a sec.

1 Like
--Client (Local Script)
local RE = game.ReplicatedStorage.GivePlayerCandyEvent --Remote Event

script.Parent.MouseButton1Down:Connect(function()
	RE:FireServer()
end)

--ServerScriptService (SCRIPT)
local PlayerDebounce = {}
local RE = game.ReplicatedStorage.GivePlayerCandyEvent

game.Players.PlayerAdded:Connect(function(Player)
	PlayerDebounce[Player] = false
end)

game.Players.PlayerRemoving:Connect(function(Player)
	PlayerDebounce[Player] = nil	
end)

RE.OnServerEvent:Connect(function(Player)
	if not PlayerDebounce[Player] then
		PlayerDebounce[Player] = true
		Player.leaderstats.Candy.Value += 1
		
		task.wait(1) --Prevent spam
		PlayerDebounce[Player] = false
	end
end)

--Selling Part script (SCRIPT)
local Part = script.Parent

Part.Touched:Connect(function(Hit)
	local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
	
	if Player then
		local leaderstats = Player:WaitForChild("leaderstats")
		local Currency = leaderstats.Gems
		local Selling = leaderstats.Candy 
		if Selling.Value > 0 then
			Currency.Value = Currency.Value + Selling.Value *10
			Selling.Value = 0
		end
	end
	
end)
1 Like

I made it work , thank you , I didn’t know you need to use Remote Events to give an player a value , thanks bro!

Use Remote event to make server do It (but exploiter can spam remote event and get infinite candy so idk)

Which is why I added debounce in my script example above.

2 Likes