So, I recently made a gun by using remote events.
I think that if I fire remote events repeatedly, the script will exhaust. If I add debounce to the server script, other clients with the same gun will experience a delay when using the gun. (I added a debounce and it will have a slight delay for the clients when using the gun)
So is there a best way to do it? And I also need suggestions for the script! (Is it better also to put a script/ a local script inside of a tool?)
This is the local script I put inside the Tool’s (Gun) Handle, which is in the StarterPack
local RS = game:GetService("ReplicatedStorage")
local GunEvent = RS:WaitForChild("GunEvent")
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local Sound = Handle:WaitForChild("Activate")
local mouse = player:GetMouse()
Tool.Activated:Connect(function()
if humanoid.Health > 0 then
Sound.Playing = true
Sound:Play()
local mousePosition = mouse.Hit.Position
local mouseTarget = mouse.Target
GunEvent:FireServer(mousePosition, mouseTarget)
print("RemoteEvent has been fired")
end
end)
And this is the server script I put inside of the ServerScriptService
local RS = game:GetService("ReplicatedStorage")
local GunEvent = RS:WaitForChild("GunEvent")
local Debounce = false
GunEvent.OnServerEvent:Connect(function(player, mousePos, mouseTarget)
if Debounce == false then
Debounce = true
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")
if humanoid.Health > 0 then
if mouseTarget and mouseTarget ~= nil then
if mouseTarget.Parent and mouseTarget.Parent ~= nil then
local EnemyHumanoid = mouseTarget.Parent:FindFirstChildOfClass("Humanoid")
if EnemyHumanoid and EnemyHumanoid.Health > 0 then
EnemyHumanoid.Health -= 20
end
end
end
end
task.wait(0.1)
Debounce = false
end
end)