Why is it firing even though it shouldn't?

Hello,
I’m making a projectile, but for some reason it still fires even though it shouldn’t I found a solution before, but since I made a custom character I’m a little confused even though I’m guessing it’s the same thing.
Here’s how my character is formatted:
Screenshot 2021-11-10 172111
The script:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player)
	local function Explosion()
		local Explosion = Instance.new("Explosion")
		Explosion.Parent = workspace
		Explosion.Position = Projectile.Position
		Explosion.BlastRadius = 0
		Projectile:Destroy()
	end
	
	Projectile.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid:TakeDamage(35)
		end
		if hit.Parent ~= Character then
			Explosion()
		end
	end)
	
	wait(1)
	Projectile:Destroy()
end)

Any help is appreciated thank you.

1 Like

Not entirely sure how your remote is set up but you can set connections to a variable and call :Disconnect() on that variable when you no longer want the connection to trigger.

Also one thing that might be of issue is your Explosion function destroys the projectile, but also at the end of the event function, it also destroys the projectile, which could error if explosion was called.

Here’s the full script btw:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player)
	local Character = player.Character
	
	local Projectile = Instance.new("Part")
	Projectile.Parent = workspace
	Projectile.Name = "Projecitle"
	Projectile.Position = Character.Direction.Position + Character.Direction.CFrame.LookVector * 15
	Projectile.Size = Vector3.new(2.5,2.5,2.5)
	Projectile.Shape = Enum.PartType.Ball
	Projectile.Material = Enum.Material.Neon
	Projectile.Color = Color3.new(0, 1, 0)
	Projectile.CanCollide = false
	
	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Parent = Projectile
	BodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	BodyVelocity.Velocity = Character.Direction.CFrame.LookVector * 100
	
	local function Explosion()
		local Explosion = Instance.new("Explosion")
		Explosion.Parent = workspace
		Explosion.Position = Projectile.Position
		Explosion.BlastRadius = 0
		Projectile:Destroy()
	end
	
	Projectile.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid:TakeDamage(35)
		end
		if hit.Parent ~= Character then
			Explosion()
		end
	end)
	
	wait(1)
	Projectile:Destroy()
end)

I think Dev_Ryan could be right, the problem you mentioned is most likely caused by the client side triggering the event randomly. Can you show us the local script of the remote?

Okay here’s the local script:

local player = game:GetService("Players").LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

local mouse = player:GetMouse()

local Holding = false

mouse.Button1Down:Connect(function()

Holding = true

end)

mouse.Button1Up:Connect(function()

Holding = false

end)

while wait() do

if Holding == true then

script.RemoteEvent:FireServer()

wait(0.5)

end

end

Firstly, it seems like your remote is stored under the local script, I suggest moving it to ReplicatedStorage as both the client and server can access it, and it wouldn’t cause further issues as a remote wouldn’t be created for every player.

Secondly, your way of listening for mouse up and down is acceptable but not too efficient and can cause unwanted errors, so I would recommend checking out
https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction

Thirdly, in your while loop check if the mouse stopped holding then use break to stop the loop.

If after doing the above the bug won’t stop then try wrapping your while loop into a spawn() or coroutine.wrap() to prevent the while loop from yielding your code.

p/s: It is way more efficient to call the loop only when the gun is firing to prevent lag issues, and mostly everything should be wrapped in context action service if you decide to use that.

p/s 2.0: If you have trouble writing the code I can help but I’m a little busy for today so I can help after around 5 hours.

1 Like
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("RemoteEvent")

re.OnServerEvent:Connect(function(player)
	local Character = player.Character
	local Projectile = Instance.new("Part")
	Projectile.Parent = workspace
	Projectile.Name = "Projecitle"
	Projectile.Position = Character.Direction.Position + Character.Direction.CFrame.LookVector * 15
	Projectile.Size = Vector3.new(2.5,2.5,2.5)
	Projectile.Shape = Enum.PartType.Ball
	Projectile.Material = Enum.Material.Neon
	Projectile.Color = Color3.new(0, 1, 0)
	Projectile.CanCollide = false

	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Parent = Projectile
	BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	BodyVelocity.Velocity = Character.Direction.CFrame.LookVector * 100

	local function Explosion()
		local Explosion = Instance.new("Explosion")
		Explosion.Parent = workspace
		Explosion.Position = Projectile.Position
		Explosion.BlastRadius = 0
		Projectile:Destroy()
	end

	Projectile.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid:TakeDamage(35)
		end
		if hit.Parent ~= Character then
			Explosion()
		end
	end)

	task.wait(1)
	Projectile:Destroy()
end)
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("RemoteEvent")
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local debounce = false

mouse.Button1Down:Connect(function()
	if debounce then
		return
	end
	debounce = true
	re:FireServer()
	task.wait(0.5)
	debounce = false
end)

Swapped the previous logic for a debounce, this should fix things (and removes the necessity of a while true do loop).

It still has the same error it explodes right away for some reason.

local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("RemoteEvent")
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local debounce = false

mouse.Button1Down:Connect(function()
	if debounce then
		return
	end
	debounce = true
	task.wait(0.5)
	re:FireServer()
	debounce = false
end)

Then just add the delay before the server is fired.

Same thing happens except with a delay. When I click a short delay happens then it just explodes. I added a print after this, and it prints “yes.” So I believe it’s something wrong with the touching bceause it thinks it’s touching the characer.

if hit.Parent ~= Character then
	print("yes")
end
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("RemoteEvent")
local debounce = false

re.OnServerEvent:Connect(function(player)
	local Character = player.Character
	local Projectile = Instance.new("Part")
	Projectile.Parent = workspace
	Projectile.Name = "Projecitle"
	Projectile.Position = Character.Direction.Position + Character.Direction.CFrame.LookVector * 15
	Projectile.Size = Vector3.new(2.5,2.5,2.5)
	Projectile.Shape = Enum.PartType.Ball
	Projectile.Material = Enum.Material.Neon
	Projectile.Color = Color3.new(0, 1, 0)
	Projectile.CanCollide = false

	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Parent = Projectile
	BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	BodyVelocity.Velocity = Character.Direction.CFrame.LookVector * 100

	local function Explosion()
		local Explosion = Instance.new("Explosion")
		Explosion.Parent = workspace
		Explosion.Position = Projectile.Position
		Explosion.BlastRadius = 0
		Projectile:Destroy()
	end

	Projectile.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if hit.Parent.Name ~= Character.Name then
				if debounce then
					return
				end
				debounce = true
				hit.Parent.Humanoid:TakeDamage(35)
				Explosion()
				task.wait(5)
				debounce = false
			end
		end
	end)

	task.wait(1)
	Projectile:Destroy()
end)
1 Like