Need help with my gun script

I made a gun gui that updates your ammo amount it works except something is wrong
1_ I tried to play a clicking sound whenever you have 0 ammo but it doesn’t work at all

2_I tried making the ammo never go below zero but I failed if I activate the weapon and the ammo is 0 than it will just go below 0 like -1, -2, -3 and so on

3_I tried to make the gun shot sound stop playing when your ammo is 0 even if you activate it but it still plays

4_ This is a hard one to explain but when you reload, the clip size doesn’t change at all
it stays 60
Here is my code:
SERVER

-- Services
local userInput = game:GetService("UserInputService")
-- Variables
local gun = script.Parent
local DamagePlayer = gun.DamagePlayer
local range = 300
local clipSize = gun.ClipSize
local MaxBullets = gun.MaxBullets.Value
local bullets = gun.Bullets
local dryFire = gun:WaitForChild("DryFire")

-- Functions
local function Reload()
	if bullets.Value < clipSize.Value and MaxBullets > 0 then
		gun.Reload:Play()
		gun.Reload.Ended:Wait()
		if MaxBullets - (clipSize.Value - bullets.Value) >= 0 then
			MaxBullets = MaxBullets - (clipSize.Value - bullets.Value)
			bullets.Value = clipSize.Value
		else
			bullets.Value = bullets.Value + MaxBullets
			MaxBullets = 0
		end
	end
end
userInput.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		Reload()
	end
end)

DamagePlayer.OnServerEvent:Connect(function(player, MousePosition, Origin)
	gun.GunShot:Play()
	if bullets.Value < 1 then
		dryFire:Play()
		gun.GunShot.Parent = workspace
		Reload()
		workspace.GunShot.Parent = gun
		dryFire:Remove()
	end
	local direction = (MousePosition - Origin).Unit * range
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {player.Character}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	local results = workspace:Raycast(Origin, direction, rayParams)
	bullets.Value = bullets.Value - 1
	if results then
		local Character = results.Instance.Parent
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid:TakeDamage(15)
		end
	end
end)

CLEINT

-- Services
local userInput = game:GetService("UserInputService")
-- Variables
local gun = script.Parent
local player = game.Players.LocalPlayer
local bullets = gun.Bullets
local MaxBullets = gun.MaxBullets
local clipSize = gun:WaitForChild("ClipSize")
local GunGui = player.PlayerGui:WaitForChild("GunGui").Bullets
GunGui.Text = tostring(bullets.Value) .. ' / ' .. tostring(MaxBullets.Value)
-- Functions
gun.Activated:Connect(function()
	bullets.Value = bullets.Value - 1
end)
local function Reload()
	if bullets.Value < clipSize.Value and MaxBullets.Value > 0 then
		gun.Reload:Play()
		gun.Reload.Ended:Wait()
		if MaxBullets - (clipSize.Value - bullets.Value) >= 0 then
			MaxBullets = MaxBullets - (clipSize.Value - bullets.Value)
			bullets.Value = clipSize.Value
		else
			bullets.Value = bullets.Value + MaxBullets
			MaxBullets = 0
		end
	end
end

userInput.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		Reload()
	end
end)

-- Shows ammo whenever the tool is equipped
gun.Equipped:Connect(function()
	GunGui.Visible = true
end)

gun.Unequipped:Connect(function()
	GunGui.Visible = false
end)

bullets.Changed:Connect(function()
	if GunGui.Text == 0 then
	else
		GunGui.Text = tostring(bullets.Value) .. ' / ' .. tostring(MaxBullets.Value)
	end
end)

Edit: Why is nobody replying? :frowning:

1 Like

I am having a bit of hard time going through your code, can you tell me where the ammo number going down is located? Client or server?

Client keeps track of the ammo amount and updates it in the gui
Server has the actual ammo

From what I can tell, there is no script to stop it from going below 0. What I could recommend is a check before running the script to - the number.

Like this:


bullets.Changed:Connect(function()
 if GunGui.Text == 0 then
 else
    GunGui.Text = tostring(bullets.Value) .. ' / ' .. tostring(MaxBullets.Value)
end
end)

Edit: sorry about my spacing I’m on mobile

1 Like

The server even has reloading function

1 Like

Nope doesn’t work still goes below zero

1 Like

I don’t think you can use user input service on the server.

userInput.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		Reload()
	end
end)
1 Like

I will try to do it in the client then

1 Like

Are there any errors? Maybe try it on the server aswell.

1 Like

There aren’t errors also i did try it in the server i moved functions and variables up and down pasted the whole client script into the server script i also pasted the server script into the client nothing happens

1 Like

Where is this Fired?

1 Like
-- Variables
local gun = script.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local DamagePlayer = gun:WaitForChild("DamagePlayer")
Mouse.Icon = "rbxassetid://409468479"

-- Functions
gun.Activated:Connect(function()
	local MousePosition = Mouse.Hit.Position
	local Origin = gun.StartPosition.Position
	DamagePlayer:FireServer(MousePosition, Origin)
end)
1 Like

Its just raycasting this gun uses raycast

1 Like

Yep, I know that.

1 Like

Have you tried printing to see if its even running the reload function

local function Reload()
        print("reloading")

	if bullets.Value < clipSize.Value and MaxBullets > 0 then
		gun.Reload:Play()
		gun.Reload.Ended:Wait()
		if MaxBullets - (clipSize.Value - bullets.Value) >= 0 then
			MaxBullets = MaxBullets - (clipSize.Value - bullets.Value)
			bullets.Value = clipSize.Value
		else
			bullets.Value = bullets.Value + MaxBullets
			MaxBullets = 0
		end
	end
end
1 Like

Yes i did print and it says reloading

1 Like

Server

-- Services
local userInput = game:GetService("UserInputService")
-- Variables
local gun = script.Parent
local DamagePlayer = gun.DamagePlayer
local range = 300
local clipSize = gun.ClipSize
local MaxBullets = gun.MaxBullets.Value
local bullets = gun.Bullets
local dryFire = gun:WaitForChild("DryFire")

-- Functions
DamagePlayer.OnServerEvent:Connect(function(player, Mouseposition, Origin)
	gun.GunShot:Play()
	local direction = (MousePosition - Origin).Unit * range
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {player.Character}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	local results = workspace:Raycast(Origin, direction, rayParams)
	bullets.Value -= 1
	if results then
		local Character = results.Instance.Parent
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid:TakeDamage(15)
		end
	end
end)

Local

-- Services
local userInput = game:GetService("UserInputService")

-- Variables
local gun = script.Parent
local player = game.Players.LocalPlayer
local bullets = gun.Bullets
local MaxBullets = gun.MaxBullets
local clipSize = gun:WaitForChild("ClipSize")
local GunGui = player.PlayerGui:WaitForChild("GunGui").Bullets
GunGui.Text = tostring(bullets.Value) .. ' / ' .. tostring(MaxBullets.Value)
local Mouse = Player:GetMouse()
local DamagePlayer = gun:WaitForChild("DamagePlayer")
Mouse.Icon = "rbxassetid://409468479"

-- Functions
gun.Activated:Connect(function()
    if bullets < 1 then
        Reload()
        return
    end
	local MousePosition = Mouse.Hit.Position
	local Origin = gun.StartPosition.Position
	DamagePlayer:FireServer(MousePosition, Origin)
end)

userInput.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		Reload()
	end
end)

-- Shows ammo whenever the tool is equipped
gun.Equipped:Connect(function()
	GunGui.Visible = true
end)

gun.Unequipped:Connect(function()
	GunGui.Visible = false
end)

bullets.Changed:Connect(function()
	GunGui.Text = tostring(bullets.Value) .. ' / ' .. tostring(MaxBullets.Value)
end)

local function Reload()
	if bullets.Value < clipSize.Value and MaxBullets > 0 then
		gun.Reload:Play()
		gun.Reload.Ended:Wait()
		if MaxBullets - (clipSize.Value - bullets.Value) >= 0 then
			MaxBullets = MaxBullets - (clipSize.Value - bullets.Value)
			bullets.Value = clipSize.Value
		else
			bullets.Value += MaxBullets
			MaxBullets = 0
		end
	end
end

Try this. Gets rid of DryFire, however you should be able to find a way to bring that back in. Alsp, I’m not sure whether MaxBullets is a value or not, so you may want to do MaxBullets.Value instead of it on its own.

1 Like

You have been trying to reply for 10 minuts

Fr bruh, mobile’s a pain…

You putted the reload function way below and you tried to call from above