the gui that keeps track of your ammo doesn’t work correctly
Here is my code
-- Services
local userInput = game:GetService("UserInputService")
-- Variables
local gun = script.Parent
local player = game.Players.LocalPlayer
local bullets = gun:WaitForChild("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)
-- Services
local userInput = game:GetService("UserInputService")
-- Variables
local gun = script.Parent
local player = game.Players.LocalPlayer
local bullets = gun:WaitForChild("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.Value
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)
-- Services
local userInput = game:GetService("UserInputService")
-- Variables
local gun = script.Parent
local player = game.Players.LocalPlayer
local bullets = gun:WaitForChild("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 - bullets.Value) >= 0 then
MaxBullets.Value -= clipSize.Value - bullets.Value
bullets.Value = clipSize.Value
else
bullets.Value += MaxBullets.Value
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)
Did not give errors also there is a server script that changes the values
-- 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
-- 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)
Oh. I think the real problem is that the Bullets value is only changing server side, while the reload system is being ran client side, which won’t have an effect on the server. I think you should either have the reload system moved to the server script, or try changing the Bullets value on the client.
I meant like change the ammo Value on a local script. If a player shoots, the client will subtract the ammo value, instead of the server.
Try using this code for your local script (and also remove the bullets.Value -= 1 from the server script on line 18).
-- Services
local userInput = game:GetService("UserInputService")
-- Variables
local gun = script.Parent
local player = game.Players.LocalPlayer
local bullets = gun:WaitForChild("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 - bullets.Value) >= 0 then
MaxBullets.Value -= clipSize.Value - bullets.Value
bullets.Value = clipSize.Value
else
bullets.Value += MaxBullets.Value
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)
bullets.Value -= 1 -- subtracting ammo on client
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)