Well, I Don’t Really Know How To Explain it but i will try my best.
so i am trying to make a simulator game and while working on the sell part
and testing it, i realized that it sells 2 times in a row even though there is no error in the output.
What do you want to achieve? Making A Simulator Sell Part
What is the issue? Sell Part Sells 2 times in a row.
local Part = script.Parent
Part.Touched:Connect(function(HIT)
local f = HIT.Parent:FindFirstChild("Humanoid")
if f then
local player = game.Players:GetPlayerFromCharacter(HIT.Parent)
if player then
local leaderstats = player:WaitForChild("leaderstats")
local currency = leaderstats.Coins
local selling = leaderstats.Swings
if selling.Value > 0 then
currency.Value = currency.Value + selling.Value * 1
selling.Value = 0
end
end
end
end)
Your touched event is activated more than once, so you will have to implement a debounce to stop it from doing anything after it has been touched for a certain period of time.
local Part = script.Parent
local debounce = false
Part.Touched:Connect(function(HIT)
if not debounce then
debounce = true
local f = HIT.Parent and HIT.Parent:FindFirstChild("Humanoid")
if f then
local player = game.Players:GetPlayerFromCharacter(HIT.Parent)
if player then
local leaderstats = player:WaitForChild("leaderstats")
local currency = leaderstats.Coins
local selling = leaderstats.Swings
if selling.Value > 0 then
currency.Value = currency.Value + selling.Value * 1
selling.Value = 0
end
end
end
debounce = false
end
end)
This is because of your debounce. It doesn’t trigger the touched event once, but multiple times when you walk over it. If you do not want this, end the debounce whenever the player steps off the sell button.-
local Part = script.Parent
local HitPlayers = {}
local TimeBetweenSells = 0.1
Part.Touched:Connect(function(HIT)
local humanoid = HIT.Parent:FindFirstChild("Humanoid")
if humanoid then
local player = game.Players:GetPlayerFromCharacter(HIT.Parent)
if player then
-- check the time
-- make sure they cant sell too quickly
if HitPlayers[player] and os.clock() - HitPlayers[player] < TimeBetweenSells then
return
end
HitPlayers[player] = os.clock()
local leaderstats = player:WaitForChild("leaderstats")
local currency = leaderstats.Coins
local selling = leaderstats.Swings
if selling.Value > 0 then
currency.Value += selling.Value
selling.Value = 0
end
end
end
end)
This uses a different type of denouncing. Let me know if this works for you