I wrote a script just for testing, it’s a projectile that simulates an explosion when it hits anything. However, when I spam it, it simply stops working, and it clones the projectile in the workspace but it no longer works, I need to reset my player for it to work again. Here are screenshots showing the problem:
local Tool = script.Parent
local Remotes = script.Parent:WaitForChild("Remotes")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Cooldown = false
Tool.Activated:Connect(function()
if Cooldown then return end
Cooldown = true
Remotes.Projectile:FireServer(Mouse.Hit)
task.wait(2)
Cooldown = false
end)
Server Script:
local Assets = game:GetService("ReplicatedStorage").Assets
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Connection
script.Parent.OnServerEvent:Connect(function(Player, Mouse)
local Projectile = Assets.Projectile:Clone()
Projectile.Parent = workspace
Projectile.CFrame = Player.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -3)
local Params = OverlapParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Player.Character, Projectile}
local StartTime = os.clock()
Connection = RunService.Heartbeat:Connect(function(dt)
local Hitted = workspace:GetPartsInPart(Projectile, Params)
Projectile.CFrame = CFrame.new(Projectile.Position) + (Mouse.Position - Projectile.Position).Unit * dt * 150
local Characters = {}
for _, part in pairs(Hitted) do
if part.Parent:FindFirstChildWhichIsA("Humanoid") and not table.find(Characters, part.Parent) then
if part.Parent.Name ~= Player.Name then
table.insert(Characters, part.Parent)
part.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
Connection:Disconnect()
Projectile:Destroy()
end
end
end
if os.clock() >= StartTime + 4 then
Connection:Disconnect()
Projectile:Destroy()
end
local Hit = false
if #Hitted ~= 0 and Hit == false then
local Tween = TweenService:Create(Projectile, TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Size = Vector3.new(60, 60, 60)})
Tween:Play()
Tween.Completed:Connect(function()
Projectile:Destroy()
end)
Hit = true
local ExplosionConnection = RunService.Heartbeat:Connect(function()
local CharactersInExplosion = workspace:GetPartsInPart(Projectile, Params)
for _, part in pairs(CharactersInExplosion) do
if part.Parent:FindFirstChildWhichIsA("Humanoid") and not table.find(Characters, part.Parent) then
if part.Parent.Name ~= Player.Name then
table.insert(Characters, part.Parent)
part.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
end
end
end
end)
Tween.Completed:Connect(function()
ExplosionConnection:Disconnect()
Projectile:Destroy()
end)
end
if Hit == true then
Connection:Disconnect()
end
end)
end)
If anyone can help me I appreciate it in advance, I’ve tried many things and I don’t understand the reason why this happens.
local Tool = script.Parent
local Remotes = script.Parent:WaitForChild("Remotes")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Cooldown = false
Tool.Activated:Connect(function()
if Cooldown then return end
Cooldown = true
Remotes.Projectile:FireServer(Mouse.Hit)
task.wait(2)
Cooldown = false
end)
Server Script:
local Assets = game:GetService("ReplicatedStorage").Assets
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Connection
script.Parent.OnServerEvent:Connect(function(Player, Mouse)
local Projectile = Assets.Projectile:Clone()
Projectile.Parent = workspace
Projectile.CFrame = Player.Character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -3)
local Params = OverlapParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Player.Character, Projectile}
local StartTime = os.clock()
Connection = RunService.Heartbeat:Connect(function(dt)
task.spawn(function()
local Hitted = workspace:GetPartsInPart(Projectile, Params)
Projectile.CFrame = CFrame.new(Projectile.Position) + (Mouse.Position - Projectile.Position).Unit * dt * 150
local Characters = {}
for _, part in pairs(Hitted) do
if part.Parent:FindFirstChildWhichIsA("Humanoid") and not table.find(Characters, part.Parent) then
if part.Parent.Name ~= Player.Name then
table.insert(Characters, part.Parent)
part.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
Connection:Disconnect()
Projectile:Destroy()
end
end
end
if os.clock() >= StartTime + 4 then
Connection:Disconnect()
Projectile:Destroy()
end
local Hit = false
if #Hitted ~= 0 and Hit == false then
local Tween = TweenService:Create(Projectile, TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Size = Vector3.new(60, 60, 60)})
Tween:Play()
Tween.Completed:Connect(function()
Projectile:Destroy()
end)
Hit = true
local ExplosionConnection = RunService.Heartbeat:Connect(function()
local CharactersInExplosion = workspace:GetPartsInPart(Projectile, Params)
for _, part in pairs(CharactersInExplosion) do
if part.Parent:FindFirstChildWhichIsA("Humanoid") and not table.find(Characters, part.Parent) then
if part.Parent.Name ~= Player.Name then
table.insert(Characters, part.Parent)
part.Parent:FindFirstChild("Humanoid"):TakeDamage(10)
end
end
end
end)
Tween.Completed:Connect(function()
ExplosionConnection:Disconnect()
Projectile:Destroy()
end)
end
if Hit == true then
Connection:Disconnect()
end
end)
end)
end)
Alright! Thanks for the patience and the responses, I really appreciate it, so far I tried alot of different things but I couldn’t find the reason of this problem.
I just want to fix the problem I described here, if you try to spam the projectile it stops and only works again if you reset your character, which is an annoying problem, I’m trying to see if the problem is the os.clock, but feel free to try to find the solution