I’m trying to make enemies spawn projectiles cloned from a folder that damage the player through the following script. The script is handled on client-side as the projectiles appear jittery or laggy when handled on the server-side.
The issue is that the Touched() event appears to activate too frequently that it runs twice before the debounce variable can be set to true. I’ve also attempted using tick() and disconnecting the event and setting it to nil at the start of the function, which produced the same results.
local module = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local AttackLib = ReplicatedStorage.Library.AttackLib
local DamageRemote = ReplicatedStorage.Remotes.DamageEvent
local BULLET_SPEED = 50
local tweenInfo = TweenInfo.new(
0.3,
Enum.EasingStyle.Sine,
Enum.EasingDirection.In
)
function module:Attack(enemy)
if enemy then
local targetFolder = AttackLib:FindFirstChild(enemy.Name)
if targetFolder then
--custom code now
local nBullet = targetFolder.Bullet:Clone()
nBullet.BodyVelocity.Velocity = enemy.Torso.CFrame.LookVector * BULLET_SPEED
nBullet.CFrame = enemy.Torso.CFrame
nBullet.Parent = workspace.DebrisHolder
local debounce = false
delay(5, function()
if nBullet ~= nil then
nBullet:Destroy()
end
end)
nBullet.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild('Humanoid')
if hum and game.Players:GetPlayerFromCharacter(hit.Parent) then
if debounce then return end
debounce = true
DamageRemote:FireServer(enemy, hum, nil, 'Magical')
nBullet.Anchored = true
TweenService:Create(nBullet, tweenInfo, {Size = Vector3.new(4,4,4), Transparency = 1}):Play()
Debris:AddItem(nBullet, 0.3)
end
end)
end
end
end
return module
Thanks in advance!