I am currently having a strange encounter with a debounce where it seems to be running more than once but not in the orderly manner you may think.
The game im working on is a tree cutting sim / open world (havent figured it out gameplay wise yet), where the mechanics are satisfying for the players.
The system i am working on is the log dropping system, here are the features in a list:
- Local log drops (only drops the logs for players cutting the tree)
- Replicates the client log on server, into the players “Logs” folder
- has a table of all the players cutting the tree and then sends the log to that client (when tree is destroyed)
- (logs are dropped in the client and moved to server when picked up)
I will show you the scripts:
Module (ran in the server): – fires it to the client to clone the logs after tree is destroyed
local module = {}
local replicatedStorage = game:GetService("ReplicatedStorage")
local logBindableEvent = replicatedStorage.TreeStorage.BindableLog
local logToClientEvent = replicatedStorage.TreeStorage.SendLogClient
local treeStorage = replicatedStorage.TreeStorage
local log = treeStorage.LogBase
module.destroyTree = function(tree)
local treePosition = tree:GetPivot().Position
tree.PrimaryPart.TreeBrokenSound:Play()
-- Gather all players cutting down the tree
local playerIDs = {}
if tree:FindFirstChild("PlayerIDs") then
playerIDs = tree.PlayerIDs:GetAttributes()
end
-- Process logs separately from player list gathering
for i = 1, tree:GetAttribute("LogRate") do
local randomOffset = Vector3.new(
math.random(-5, 5),
0,
math.random(-5, 5)
)
-- Fire log event to all players involved
for id, v in pairs(playerIDs) do
local player = game.Players:GetPlayerByUserId(v)
if player then
logToClientEvent:FireClient(player, log, tree, treePosition, randomOffset)
end
end
for i, v in pairs(tree:GetChildren()) do
if v:IsA("BasePart") or v:IsA("UnionOperation") or v:IsA("Part") then
local tweenService = game:GetService("TweenService")
local properties = {
Transparency = v.Transparency + 1
}
local info = TweenInfo.new(.5)
local track = tweenService:Create(v, info, properties)
track:Play()
track.Completed:Connect(function ()
if tree ~= nil then
wait(.5)
tree:Destroy()
end
end)
end
end
wait()
end
end
return module
Client – clones the log onto the target player client
local sendLogClient = game.ReplicatedStorage.TreeStorage.SendLogClient
sendLogClient.OnClientEvent:Connect(function (log, tree, treePosition, randomOffset)
local clonedLog = log:Clone()
clonedLog.Parent = workspace.LocalDroppedLogs
clonedLog.Name = tree:GetAttribute("LogType") .. "Log"
clonedLog.Position = treePosition + randomOffset
clonedLog.Orientation = Vector3.new(math.random(1, 360), math.random(1, 360), math.random(1, 360))
clonedLog.Size = tree:GetAttribute("LogSize")
clonedLog.BrickColor = tree:GetAttribute("LogColour")
clonedLog:SetAttribute("SellMultiplier", tree:GetAttribute("SellMultiplier"))
end)
LogModule (ran in the client) – can see the local players logs and fires to server after player has touched the local log.
local module = {}
local tweenService = game:GetService("TweenService")
local sendLogStats = game.ReplicatedStorage.TreeStorage.SendLogStats
module.checkLogs = function(logFolder)
for i, v in pairs(logFolder:GetChildren()) do
local debounce = false
v.Touched:Connect(function (otherPart)
local collectionSFX = v.CollectionSound
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if humanoid then
local character = otherPart.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local info = TweenInfo.new(.5)
local properties = {
Size = v.Size - Vector3.new(5, 5, 5),
CFrame = humanoidRootPart.CFrame
}
local track = tweenService:Create(v, info, properties)
local logsFolder = player:FindFirstChild("Logs")
if logsFolder and not v:GetAttribute("IsStoraged") then
track:Play()
collectionSFX.PlayOnRemove = true
track.Completed:Connect(function ()
wait()
sendLogStats:FireServer(v:GetAttribute("IsStoraged"), v:GetAttribute("SellMultiplier"), v.BrickColor)
v:Destroy()
end)
end
end
end
end)
end
end
return module
Server – ran when player has touched the log, this is where the issue consists
local replicatedStorage = game:GetService("ReplicatedStorage")
local logBindableEvent = replicatedStorage.TreeStorage.BindableLog
local sendLogClient = replicatedStorage.TreeStorage.SendLogClient
local sendLogStats = replicatedStorage.TreeStorage.SendLogStats
local debounce = false
sendLogStats.OnServerEvent:Connect(function (player, isStoraged, sellMultiplier, brickColor)
local logsFolder = player:FindFirstChild("Logs")
print("event fired to server")
if not debounce then
debounce = true
print("event fired to server, with debounce")
local clonedLog = replicatedStorage.TreeStorage.LogBase:Clone() -- replicate the local log
clonedLog.CanTouch = false
clonedLog.Parent = logsFolder
clonedLog:SetAttribute("SellMultiplier", sellMultiplier)
clonedLog:SetAttribute("IsStoraged", isStoraged)
wait(.1)
debounce = false
end
end)
In the video below, when you pickup one log it clones 2? and when you touch 100 logs, it only clones around 60? its really inconsistent.