Need help with my gun script

Yep. Functions can work like that.

It doesn’t it shows red lines under reload

:person_shrugging: Maybe I’m wrong.
Try moving the function back up.

Yep it stopped showing red lines time to test it

After the game loaded it showed the error Bullets is not a valid member of Tool “Players.LordBoboux.Backpack.Revolver”

Ok… add it then. :smiley:

I will start fixing the errors in output myself since they are easy to understand

Feel free to reply if you gey any errors that you can’t figure out.

Hold on something weird happens I will try to record a video and convert it to mp4 to make you better understand what’s happening

This is what’s happening:
So sorry if there is lag

Hello? are you there? I am waiting…

I don’t have that much time please hurry

Please be patient, I’m trying to figure out the source of the error.

Yep, I’m here now.

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

gun.Activated:Connect(function()
    if bullets.Value < 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)

Not sure if this’ll work. Add prints all around and inside the Reload function and try debug it.

Hey sorry for late reply roblox studio gltched also it did not work at all

What did you change? I don’t see anything changed

:man_shrugging: I’m genuinely confused. Sorry, but I don’t think I can help you out today.

I have another possible idea. I think the problem is the client (or server) still thinks the bullets are 0, while the server (or client) thinks the reloaded bullets, which is 30.

I think you’re onto something…
@LordBoboux try this as the server script:

-- 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)
	if results then
		local Character = results.Instance.Parent
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid:TakeDamage(15)
		end
	end
end)