Very buggy and irregular gun system

Basically, the damage first of all only works some of the time on the gun.
Secondly, the gui is EXTREMELY buggy
Sometimes it works, but sometimes it just glitches out.
I’m extremely confused as I’ve coded it the way it’s intended to be coded, but it just doesn’t work.

Here are the scripts, I just need to fix the weird irregularity and bugginess of this.

Serverscript

Script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local rt = replicatedstorage:WaitForChild("Shoot")
local serverscriptserver = game:GetService("ServerScriptService")
local module = require(serverscriptserver.DamageModule)
local soundservice = game:GetService("SoundService")
local sound = soundservice["Gun Shot"]
rt.OnServerEvent:Connect(function(plr, mouse, tool,pos, targetfilter, ishooting)
	local muzzle = tool.Effect.MuzzleEffect
	--- print(targetfilter)
	targetfilter = tool.Handle
module.IdentifyGun(tool.Name, mouse, muzzle, plr.Name, sound, plr)
end)
Module

Module:

local damage = {}
local config
function damage.IdentifyGun(gunname, target, muzzleeffect, plr, gunshot, player)
	local character = player.Character or player.CharacterAdded:Wait()
config = script.Configuration
local folder = config:FindFirstChild(gunname)
local damagevar = folder.Damage
local ammovar = folder.Ammo
local counter = 0
local ammo = character:FindFirstChild(gunname .. " ammo")
if ammo == nil then return end
local debounce = character:FindFirstChild(gunname .. " debounce")
local fireratevar = folder.FireRate
local reloadtimevar = folder.ReloadTime
local maxammovar = folder.MaxAmmo
if target == nil then
	return
end
		if ammo.Value > 0 and debounce.Value == false then
			ammo.Value -=1
		debounce.Value = true
		muzzleeffect.Enabled = true
		gunshot:Play()
		task.wait(fireratevar.Value)
		debounce.Value = false
		muzzleeffect.Enabled = false
		if target.Parent:FindFirstChild("Humanoid") then
			target.Parent.Humanoid:TakeDamage(damagevar.Value)
		end
			if ammo.Value == 0 then
				debounce.Value = true
				task.wait(reloadtimevar.Value)
				ammo.Value = maxammovar.Value
				debounce.Value = false
				return
			end
end
end
return damage
Local script

Script:

local remaining = game.ReplicatedStorage.Remaining
local reloadrt = game.ReplicatedStorage.Reload
local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent
local playergui = plr.PlayerGui
local firerate = 0.4
local debouncereload = false
local uis = game:GetService("UserInputService")
local is_shooting = false
local debounce = false
local maxammo = 26
local amount = 0
local value = plr.Bulletpack.Bullet
local character = plr.Character or plr.CharacterAdded:Wait()
local ammo_out = false
local rt = game.ReplicatedStorage:FindFirstChild("Shoot")
local yield = 0.1
local ammo = 26
local gui = plr.PlayerGui:WaitForChild("GUIGLOCK")
uis.InputBegan:Connect(function(input, fals)
if input.UserInputType == Enum.UserInputType.MouseButton1 and plr.Character:FindFirstChild(script.Parent.Name) then
		is_shooting = true
		while is_shooting == true and debounce == false do
			if ammo <= 0 then
				gui.Display.Value.Text = "Reloading"
				debounce = true
				task.wait(2)
				debounce = false
				ammo = maxammo
			end
			ammo -= 1
			gui.Display.Value.Text = ammo .. "/".. maxammo
			task.wait(yield)
			rt:FireServer(mouse.Target,script.Parent,mouse.Hit.Position, mouse.TargetFilter, is_shooting)
		end
	end
end)
uis.InputEnded:Connect(function(input, fals)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and  plr.Character:FindFirstChild(script.Parent.Name) then
		is_shooting = false
	end
end)
tool.Equipped:Connect(function()
	gui.Enabled = true
end)
tool.Unequipped:Connect(function()
	gui.Enabled = false
end)
1 Like

Try replacing your local script with this

local remaining = game.ReplicatedStorage.Remaining
local reloadrt = game.ReplicatedStorage.Reload
local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent
local playergui = plr.PlayerGui
local firerate = 0.4
local debouncereload = false
local uis = game:GetService("UserInputService")
local is_shooting = false
local debounce = false
local maxammo = 26
local amount = 0
local value = plr.Bulletpack.Bullet
local character = plr.Character or plr.CharacterAdded:Wait()
local ammo_out = false
local rt = game.ReplicatedStorage:FindFirstChild("Shoot")
local yield = 0.1
local ammo = 26
local gui = plr.PlayerGui:WaitForChild("GUIGLOCK")
uis.InputBegan:Connect(function(input, fals)
if input.UserInputType == Enum.UserInputType.MouseButton1 and plr.Character:FindFirstChild(script.Parent.Name) then
		is_shooting = true
		while is_shooting == true do
			if ammo <= 0 and not debounce then
				gui.Display.Value.Text = "Reloading"
				debounce = true
				task.wait(2)
				debounce = false
				ammo = maxammo
			end
			ammo -= 1
			gui.Display.Value.Text = ammo .. "/".. maxammo
			task.wait(yield)
			rt:FireServer(mouse.Target,script.Parent,mouse.Hit.Position, mouse.TargetFilter, is_shooting)
		end
	end
end)
uis.InputEnded:Connect(function(input, fals)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and  plr.Character:FindFirstChild(script.Parent.Name) then
		is_shooting = false
	end
end)
tool.Equipped:Connect(function()
	gui.Enabled = true
end)
tool.Unequipped:Connect(function()
	gui.Enabled = false
end)
```**strong text**

Now the server script just completely failed and the ammo goes into the negatives, what is going on

everything about calculations and ect are server sided, client is only detecting when player press R to reload and handle animation&effects, raycasting, detecting tool activation or damage is on server!!!

I do not understand what you’re saying

Hes saying to put all the stuff on the server and the client only detects key presses

edit: because exploiters can change whatever they want on the client

But I don’t understand how an exploiter could change that to their benefit.
Even then, this problem still isn’t fixed

They can fire remote events

So they can fire the rt event and hit everyone at infinite speed

The server script has a debounce, completely preventing that problem

Look I’d much like you to appreciate to fix the problem at hand, then we could discuss the potential server script security issue.

I appreciate you trying to spot out this problem though

Can anybody actually help me with this?