Gui doesn't work

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)

This is what happens instead


-- 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)

What did you change?
That didn’t work at all

Try and see if this code works:

-- 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)

Nope that didn’t work did you see the video?

Yes I have seen the video. Are you receiving any errors in the output? And also, is there any other script that could possibly change the values?

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)

its at line 18

1 Like

I think that is the problem. Try removing the bullets.Value -= 1 line from the server script, see if that does anything.

Well after i removed it the gui never changes its always 30 / 60

1 Like

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.

Well how can i do that?
How can i move reload system to the server?

First try changing the Bullets value on the client, under the gun.Activated event in the local script.

Wait i tried copying the reload function from client to the server
so that glitch start happening to the clip size the 60 in 30 / 60
What do i do now?

So i guess its a problem with the gui maybe?

Did you try changing the Bullets value on the client?

No, i didn’t because i didn’t understand what you meant by that
How can i change 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)

Omg that worked thank you so much

1 Like

Can you explain to me what you changed?
So i don’t make the same mistake

I just added the bullets.Value -= 1 on line 39 in the local script.