Coin cannot get collected

The coin won’t get collected, it won’t change or disappear it just won’t fix.

WaitTime = 120 -- How many seconds to respawn the coin!
Amount = 1 -- Amount of coins per coin!

local debounce = false

function onTouched(part)
	if not debounce then
		debounce = true
		local h = part.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("EggCoins")
					if (Score~=nil) then
						Score.Value = Score.Value + Amount
					end
				end
			end
			script.Parent.Music:Play()
			script.Parent.Day.Enabled = true
			script.Parent.Transparency = 1
			wait(1.25)
			script.Parent.Day.Enabled = false
			script.Disabled = true
			wait(WaitTime)
			debounce = false
			script.Parent.Transparency = 0
			script.Disabled = false
		end
	end
end

script.Parent.Touched:Connect(onTouched)

image

1 Like

Here are a few questions regarding this bug:


  • Which script are you using?
  • What object-type is script.Parent
  1. Regular Script

  2. MeshPart

I guess

The problem was most likely the part where you tried finding the player.

Instead of finding the player via the character’s name, I suggest just using the character itself: Players:GetPlayerFromCharacter(Humanoid.Parent)

–which is my preferred way to find the player via touch events


----- | Services | -----
local Players = game:GetService("Players")


----- | Variables | -----
WaitTime = 120
Amount = 1
Debounce = false


----- | Method | -----
function onTouch(part)
	if not Debounce then Debounce = true
		local Human = part.Parent:FindFirstChild("Humanoid")
		
		if Human then
			local Player = Players:GetPlayerFromCharacter(Human.Parent)
			
			if Player then
				local leaderStat = Player:FindFirstChild("leaderstats") :FindFirstChild("EggCoins")
				
				if leaderStat then
					leaderStat.Value += Amount
				end
			end
			
			script.Parent.Music:Play()
			script.Parent.Day.Enabled = true
			script.Parent.Transparency = 1
			
			task.wait(1.25)
			script.Parent.Day.Enabled = false
			script.Disabled = true
			
			task.wait(WaitTime)
			script.Parent.Transparency = 0
			script.Disabled = false	
			
			debounce = false
		end
	end
end


----- | Callback | -----
script.Parent.Touched:Connect(onTouched)
1 Like

IT works! Tysm.

thiry char thing

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