Need help with subtraction on collide script

Okay so, when I add the the line of math to substract the money, it substracts the money, but the it’s like the rest of the code stops working, it doesn’t change the transparency or the collider… those will work without the line thought, what am I doing wrong?

My script:

local RC = 100000 --Required Coins to unlock

local chill = false --debounce

script.Parent.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then

	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if player.leaderstats.Coins.Value >= RC then --Enter your own currency name instead of "Coins"

		if not chill then

			chill = true
			
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 100000 --THIS LINE
			
			script.Parent.Transparency = 1 --Full transparency
			
			script.Parent.SurfaceGui.TextLabel.Visible = false --hide the textlabel, must be named TextLabel

			script.Parent.CanCollide = false
			
			if player.leaderstats.Coins.Value <= RC then

				script.Parent.Transparency = 0 --No transparency 
				
				script.Parent.SurfaceGui.TextLabel.Visible = true --Show the textlabel, must be named TextLabel

				script.Parent.CanCollide = true
				
				wait(2)-- Spam protector

				chill = false -- Spam protector	
				
			end

		end

	end

end

end)

It could be that you are improperly using debounces.

I’ve noticed that you left the variable chill wrapped into another conditional statement. That’s not good. Your conditional statements also seem to be pretty messy.

Try this:

local RC = 100000 --Required Coins to unlock
local chill = false --debounce

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		if not chill then
			chill = true
			
			if player.leaderstats.Coins.Value >= RC then --Enter your own currency name instead of "Coins"
				player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 100000 --THIS LINE
				script.Parent.Transparency = 1 --Full transparency
				script.Parent.SurfaceGui.TextLabel.Visible = false --hide the textlabel, must be named TextLabel
				script.Parent.CanCollide = false

			elseif player.leaderstats.Coins.Value <= RC then
				script.Parent.Transparency = 0 --No transparency 
				script.Parent.SurfaceGui.TextLabel.Visible = true --Show the textlabel, must be named TextLabel
				script.Parent.CanCollide = true
			end
			
            wait(1) --will help prevent spam

			chill = false
		end
	end
end)

Edit: added a wait after the conditional statements.

Ah thank you so much! It worked, and sorry for the mess, I was trying different things to make it work and im still really new to scripting.

1 Like