Help with coins

Hi! watch this video:
robloxapp-20200418-1700065.wmv (692.9 KB)
and here is my output printing out “hit” every time I hit the coin:
Opomba 2020-04-18 170145
but I want to make it so I can only touch the coin once and any further touch is not detected. Here is my script:

local coins = workspace.Coins:GetChildren()
local tw = game:GetService("TweenService")
local info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, math.huge, true, 0)
local info2 = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In)


for i, v in pairs(coins) do
	local goal = {Position = v.End.Position}
	local tween = tw:Create(v.Coin, info, goal)
	tween:Play() -- DEFOULT TWEENING
	v.Coin.Touched:Connect(function(hit)
		print(hit)
		local goal2 = {Position = v.Fade.Position}
		local tween2 = tw:Create(v.Coin, info2, goal2)
		tween2:Play() -- TWEEN THAT PLAYS WHEN I TOUCH A COIN
		for i = 0, 1, 0.01 do  -- THIS MAKES A COIN FADE
			wait()
			v.Coin.Transparency = i
		end
		v:Destroy() -- DESTROYING THE COIN MODEL
		coins = workspace.Coins:GetChildren() -- CLEARING THE COIN MODEL FROM THE TABLE
	end)
end

game:GetService("RunService").Heartbeat:Connect(function(frame)	
	for i, v in pairs(coins) do -- FOR EVERY COIN IN THE TABLE DO SPINNING
		v.Coin.CFrame = v.Coin.CFrame * CFrame.Angles(math.rad(1)*frame*60, 0, 0)
	end
end)

I already have tried debounce outside the for loop, but its pointless:
robloxapp-20200418-1711295.wmv (2.1 MB)
and if make debounce inside the touched function then I am setting it every time I touch a coin back to false and that’s not great either… Thanks for ur help!
Also here is my workspace:
Opomba 2020-04-17 181056
script is inside of ServerScriptService.

Add something like a BoolValue into your coins, then detect if the value is false, set it to true on touching.

So because you are using for loop in GetChildren() function this might be used by performance but anyways here is what you have to do, add bool value and flags to prevent multiple injections:

local coins = workspace.Coins:GetChildren()
local tw = game:GetService("TweenService")
local info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, math.huge, true, 0)
local info2 = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local flag = false

for i, v in pairs(coins) do
    local Bool = Instance.new("BoolValue")
    Bool.Name = "TouchedPlayer"
    Bool.Parent = v.Coin
end

for i, v in pairs(coins) do
	local goal = {Position = v.End.Position}
	local tween = tw:Create(v.Coin, info, goal)
	tween:Play() -- DEFOULT TWEENING
	v.Coin.Touched:Connect(function(hit)
		print(hit)
      if v.Coin.TouchedPlayer.Value ~= true and flag ~= true then
        flag = true
		local goal2 = {Position = v.Fade.Position}
		local tween2 = tw:Create(v.Coin, info2, goal2)
		tween2:Play() -- TWEEN THAT PLAYS WHEN I TOUCH A COIN
		for i = 0, 1, 0.01 do  -- THIS MAKES A COIN FADE
			wait()
			v.Coin.Transparency = i
		end
		v:Destroy() -- DESTROYING THE COIN MODEL
		coins = workspace.Coins:GetChildren() -- CLEARING THE COIN MODEL FROM THE TABLE
        v.Coin.TouchedPlayer.Value = true
        flag = false
        end
	end)
end

game:GetService("RunService").Heartbeat:Connect(function(frame)	
	for i, v in pairs(coins) do -- FOR EVERY COIN IN THE TABLE DO SPINNING
		v.Coin.CFrame = v.Coin.CFrame * CFrame.Angles(math.rad(1)*frame*60, 0, 0)
	end
end)
1 Like