Coin giver script isn't working

Hello! So I was trying to make it so when the player finishes a level and tp’s back then they get 10 coins BUT WHENEVR I TOUCH IT IT GIVES FREAKING 70, I tried to add a debounce script but this came up in the output, please help.

Output

 12:25:14.512  Workspace.Telepart (  A Walk In A Park!  FINISHED).Script:23: Expected 'end' (to close 'function' at line 4), got <eof>; did you forget to close 'then' at line 14?  -  Studio - Script:23
  12:25:25.391  HE  -  Studio
  12:25:25.850  NameTag is not a valid member of Script "ServerScriptService.adminCommands"  -  Server - adminCommands:49
  12:25:25.851  Stack Begin  -  Studio
  12:25:25.851  Script 'ServerScriptService.adminCommands', Line 49  -  Studio - adminCommands:49
  12:25:25.851  Stack End  -  Studio
  12:25:28.075  0.5, 0.5  -  Client
  12:25:28.357  0.5, 0.5  -  Server

Coin Giver Script

local DB = false

Amount = 10 -- Amount of coins per coin!
function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not DB then
		DB = true
	local h = hit.Parent:findFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:FindFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local Score = stats:findFirstChild("Star Coins")
				if (Score~=nil) then
					Score.Value = Score.Value + Amount
						DB = false
				end
			end
		end
	end
end

script.Parent.Touched:Connect(onTouched)

You lack only one end to close the if statement. Add one more.

2 Likes

I did that and… It gave me like 70 freaking coins…

Im trying to add a delay so that players wont get thousands of coins when they complete a level

This article has an example of how to implement a simple cooldown for something like this.

This is the basic implementation of a cooldown

local healthPack = script.Parent
local healAmount = 30
local cooldown = 10
local canHeal = true

local function onPartTouch(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and canHeal == true then
		-- Player character touched health pack
		canHeal = false
		local currentHealth = humanoid.Health
		local newHealth = currentHealth + healAmount
		humanoid.Health = newHealth
		wait(cooldown)
		canHeal = true
	end
end
healthPack.Touched:Connect(onPartTouch)
1 Like

Indent all the if statements, there is an if statement lined up below a consecutive if statement at the top. And then add the missing end.

local cooldown = 10

Amount = 10 -- Amount of coins per coin!
function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not DB then
		DB = true
	local h = hit.Parent:findFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:FindFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local Score = stats:findFirstChild("Star Coins")
					if (Score~=nil) then
						wait(cooldown)
						Score.Value = Score.Value + Amount
					end
				end
			end
		end
	end
end

script.Parent.Touched:Connect(onTouched)

:+1: