Firing Server only once?

I want to create an explosion anywhere I click with my mouse. You can only get the mouse from local scripts, so I needed to make a remote event. So when I click it fires the server with my mouses position and creates an explosion in that area.

local script:

local player = game.Players.LocalPlayer

local repStorage = game:GetService(“ReplicatedStorage”)

local bomb = repStorage:WaitForChild(“Bomb”)

local bombRemote = repStorage:WaitForChild(“BombRemote”)

local db = false

local mouse = player:GetMouse()

mouse.Button1Up:Connect(function()

if db == false then

db = true

bombRemote:FireServer(mouse.Hit)

end

end)

Server Script:

local repStorage = game:GetService(“ReplicatedStorage”)
local bombRemote = repStorage:WaitForChild(“BombRemote”)

bombRemote.OnServerEvent:Connect(function(…)
local Tuple = {…}
local player = Tuple[1]
local character = player.Character
local hit = Tuple[2]
local bomb = Instance.new(“Explosion”)
bomb.Position = hit.Position
bomb.Parent = game.Workspace
end)

I fire the server when I click, but only on the first click. When I try to click again it does nothing.

Any ideas on how to fix this?

1 Like

Any error messages after the first click?

It works during the first click because db is still false. When you clicked it set to true, meaning the condition will be false the second time you click so it will not be fired the second time.

The fix:
Just add this above end:

task.wait(cooldown) --replace cooldown with cooldown, or 1 if you like
db = false

Wow! I should have looked over my script a second time. lol

1 Like