I'm making a gun, and when I reload it's supposed to make the interger "Ammo" equal to "MaxAmmo" (Value-wise). However, it does not equal to it. Any help?

Local Script:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local Tool = script.Parent
local Reload = Tool:WaitForChild("Reload")
local Shoot = Tool:WaitForChild("Shoot")
local IsEquipped = false

local IsReloading = false
local IsShooting = false

local Damage = 25
local FiringRate = 1.5
local Accuracy = 0.5

local Ammo = Tool:WaitForChild("Ammo")
local MaxAmmo = Tool:WaitForChild("MaxAmmo")
local Gunshot = Tool:WaitForChild("Gunshot")

Tool.Equipped:Connect(function()
	IsEquipped = true
end)

Tool.Unequipped:Connect(function()
	IsEquipped = false
end)

Tool.Activated:Connect(function()
	if not IsShooting and Ammo.Value > 0 and not IsReloading then
		IsShooting = true
		Fire()
		wait(FiringRate)
		IsShooting = false
	end
end)

function Fire()
	local ray = Ray.new(Tool.Handle.CFrame.p, (Mouse.Hit.p - Tool.Handle.CFrame.p + Vector3.new(math.random(0-Accuracy,Accuracy),math.random(0-Accuracy,Accuracy),0)).unit * 300)
	local Part, Position = workspace:FindPartOnRay(ray, Player.Character, false, true)
	Shoot:FireServer(Tool,Position,Part,Damage)
	Ammo.Value = Ammo.Value - 1
	Gunshot:Play()
	print("Ammo: "..Ammo.Value.."")
end

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		Reload:FireServer()
		IsReloading = true
		wait(1.018)
		IsReloading = false
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Server-Sided Script:

local Tool = script.Parent
local Reload = Tool:WaitForChild("Reload")
local Shoot = Tool:WaitForChild("Shoot")
Reload.OnServerEvent:Connect(function()
	Tool.ReloadGun:Play()
	wait(1.018)
	Tool:WaitForChild("Ammo").Value = Tool:WaitForChild("MaxAmmo").Value
	print("Ammo: "..Tool:WaitForChild("Ammo").Value.."")
end)
Shoot.OnServerEvent:Connect(function(Player,Tool,Position,Part,Damage)
	local beam = Instance.new("Part", workspace)
	beam.BrickColor = BrickColor.new("Medium stone grey")
	beam.Material = "Metal"
	beam.Transparency = 0.25
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false		
	local distance = (Tool.Handle.CFrame.p - Position).magnitude
	beam.Size = Vector3.new(0.3, 0.3, distance)
	beam.CFrame = CFrame.new(Tool.Handle.CFrame.p, Position) * CFrame.new(0, 0, -distance / 2)		
	game:GetService("Debris"):AddItem(beam, 0.1)
	if Part then
		local humanoid = Part.Parent:FindFirstChild("Humanoid")	
		if not humanoid then
			humanoid = Part.Parent.Parent:FindFirstChild("Humanoid")
		end	
		if humanoid then
			humanoid:TakeDamage(Damage)
		end
	end
end)

Edit: There are no errors.

14 comments

0

I mean if the Ammo's value is being set to five properly, then the issue has nothing to do with the code you have posted.

0

Is MaxAmmo zero?

0

Your script seems to be making the ammo be equal to the maxammo. I see no problem. Maybe there's something else that sets the Ammo Value to zero or something that isn't 5. (Also you can remove the .."" after the Tool.Ammo.Value in the print() line because it's redundant)

0

Are you changing the value client sided? If you are, the server will not recognise this change. Just set it to 5 to test (replace Tool.MaxAmmo.Value to just 5)

0

@SummerEquinox that's what I intially thought, but I analyzed the code in both and I see no problems.

0

@incapaz No, I checked.

0

@incapaz I'm doing it in a server-script.

0

Can you please post more code? With only this to go off of, it's quite hard to figure out what your problem is. The most likely reason for this is what incapaz said, if you are trying to change the value on the client, the server will not recognize the change.

0

@CPF2 Okay, I'll try.

0

^ or value updates aren't being caught well

0

Just read your code; you're changing the ammo on the client side. Do it server sided, and off topic, but an exploiter can tell the server to instantly kill a player, don't send the damage. The server should determine how much to damage, not a client.

0

Also why are Fire and onKeyPress global variables?

1

@incapaz Thanks! It just thinks that there's no more bullets left when there's only a single bullet left.

0

o nvm it works now, thanks though