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:
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)
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.
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?
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.
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.
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).
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.