Annoying problem with my projectile

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:


Screenshot_2

Localscript:

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.

5 Likes

Have you tried using task.spawn? It basically allows you to run multiple pieces of code at the same time in one script.

task.spawn(function()
    -- Code to run alongside
end)
1 Like

I tried here, didn’t fix at all.

I implemented it like:

image

Thanks for the response!

2 Likes

I don’t know the fix yet, but it lies somewhere in this snippet of code (at least I think):

	if os.clock() >= StartTime + 4 then
			print("Os")
			Connection:Disconnect()
			Projectile:Destroy()
		end
2 Likes

No, I would try this:

Local Script:

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)
2 Likes

Apparently you’re right, I changed to yours and after the time, the projectiles stopped and I got it:

What should I do?

1 Like

Same problem yet…

image

2 Likes

Hm, I guess I was wrong about the task.spawn, I’ll look in a bit more.

2 Likes

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.

2 Likes

May I see the game so I can also test it too, or no?

2 Likes

Of course! The game link: Game

2 Likes

Alright, thanks, I’ll open studio.

1 Like

Add me on roblox, so I’ll be able to add you in the studio, so you’ll be able to see the problem. So far, I’m trying to find solutions.

1 Like

Alright, I added you as a friend, so I think you can now add me to the game.

1 Like

I did, you can acess the game in the studio now!

1 Like

So, how exactly did you want it? From your description, it sounds like what you wanted in its current form.

Edit: It just broke

2 Likes

trying your game rn, no issues.

edit: nvm

2 Likes

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

2 Likes

The problem happens when you spam the projectile, it doesn’t work anymore until you reset your character

1 Like

why are you using :disconnect(), wouldn’t :Destroy() be more ideal?

2 Likes