Recently I have been making a gun game and I’ve made what I wanted but I tried making a gun system and I can’t seem to make one. My current idea is to make a int value and subtracting it for every shot. I would then check before each shot if the value is > than 0. I then used a local script and using user input services I would detect if R(the key to reload) is pressed and then set the value of the int value back to the desired number. However It doesn’t seem to work and I think the problem is that int values cannot be changed by local scripts. Is there a way to use my method or a different method to make a ammo system. I am a new scripter so if it is a simple fix I won’t be surprised.
Here is the script that makes the raycast
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local RunService = game:GetService("RunService")
local Tool:Tool = script.Parent
local Handle = script.Parent.Handle
local u1 = script.Parent.Union
local u2 = script.Parent.Union2
local ShootPart = script.Parent.Part
local RemoteEvent = Tool:FindFirstChild("RemoteEvent")
local OnCooldown = false
local pe = ShootPart.ParticleEmitter
local sl = ShootPart.SpotLight
local chamber = script.Parent.Chamber
local dead = false
local plrs = game:GetService("Players")
local Do = {
Damage = 18;
Cooldown = 0.3;
Visualize = true;
ShootSound = "rbxassetid://5238024665";
}
while true do
local Origin = ShootPart.Position
wait(0.01)
RemoteEvent.OnServerEvent:Connect(function(Player,Received)
local leaderstats = Player.leaderstats
local kills = leaderstats.Kills
if not OnCooldown then
if chamber.Value > 0 then
OnCooldown = true
task.delay(Do.Cooldown,function()
OnCooldown = false
end)
chamber.Value = chamber.Value - 1
local Direction = (Received.Position-Origin).Unit*3000
local Raycast = workspace:Raycast(Origin,Direction)
local Intersection = Raycast and Raycast.Position or Origin + Direction
local Distance = (Origin - Intersection).Magnitude
pe.Enabled = true
sl.Brightness = 28
wait(0.1)
pe.Enabled = false
sl.Brightness = 0
if (Do.ShootSound ~= "" or Do.ShootSound ~= nil) then
local Sound = Instance.new("Sound")
Sound.SoundId = Do.ShootSound
Sound.Volume = .65
Sound.PlayOnRemove = true
Sound.Parent = Handle
Sound:Destroy()
end
if Raycast then
local Hit:Part = Raycast.Instance
local Humanoid = (Hit.Parent:FindFirstChildOfClass("Humanoid") or Hit.Parent.Parent:FindFirstChildOfClass("Humanoid"))
if Humanoid and Humanoid.Parent ~= Player.Character then
if Humanoid.Health > 0 then
dead = false
Humanoid:TakeDamage(Do.Damage)
script["HitMarker Sound Effect"]:Play()
if Humanoid.Health <= 1 then
if dead == false then
script["arsenal kill sound"]:Play()
script["Kill sound"]:Play()
kills.Value = kills.Value + 1
dead = true
end
end
end
end
end
end
end
end)
end
Here is the local script that reloads the gun
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local equip = false
equip = true
uis.InputBegan:Connect(function(hit)
if hit.KeyCode == Enum.KeyCode.R then
script.Parent.Chamber.Value = 9
print("reloaded")
script.r:Play()
end
end)
The Explorer of the gun
Any help would be great and I would be really thankful. Anyways have a good day internet.