mc7oof
(oof)
March 1, 2023, 2:54am
#42
I added notes for myself. In my copy of your file. Sorry, it’s the 2A where the confusing change is made.
Also, I dont understand what the goal of 2B is. It looks like they are giving you more ammo than the original amount, but I’m not sure when that else
would ever happen.
Comqts
(Connor)
March 1, 2023, 2:54am
#43
Alright. I do have to head to bed soon here, so I’ll get back to you as soon as I can tomorrow.
Comqts
(Connor)
March 1, 2023, 11:00pm
#44
Any ideas on how I could fix this?
mc7oof
(oof)
March 2, 2023, 12:28am
#45
I know how to get around the problem. Replace your reload() function with the following:
local function reload()
script.Parent.MaxAmmo.Value = 30
clipSize = 30
RefreshGui()
canshoot = true
end
The other parts of the function don’t make any sense to me so I just delete them to help get you onto your next project.
There is a wait() somewhere else in the script that causes a delay after reload, so give it a second or two after reloading to make sure it works.
Comqts
(Connor)
March 2, 2023, 10:40pm
#46
For some reason, it’s not working
Comqts
(Connor)
March 4, 2023, 12:28am
#47
It didn’t end up working, anything else?
mc7oof
(oof)
March 4, 2023, 12:43am
#48
The code I pasted above worked for me just fine.
There is something wrong in the current relaod() function you are using. It is too confusing for me to try and figure out, but I replaced it with the code above and it worked fine.
There is a delay after reloading, so give it a few second before you decide it isn’t working.
Then find the delay and remove it.
Comqts
(Connor)
March 6, 2023, 2:14am
#49
could ya download the copy of the game for me and send it back rq?
Comqts
(Connor)
March 7, 2023, 10:00pm
#50
Can you copy the game and send it back?
Comqts:
I’m making a gun system, my gun shoots just fine, aims fine, etc. My issue is that it only reloads once, then it doesn’t reload anymore after that. I’m trying to make it so you can completely run out of ammo. So here’s an example.
16/30 Bullets
16 being the ammo thats being shot, 30 being the mag size
after i reload, it should be 30/14
Since it’s back to 30 bullets to shoot, and 14 is leftover in the mag for the next reload, the next reload would change from 0/14 to 14/0. Any fixes?
local runService = game:GetService("RunService")
local userinputservice = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = workspace:WaitForChild(player.Name)
local humanoid = char:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local cam = workspace.Camera
local main = script.Parent:WaitForChild("Handle")
--variables
local walkf = false
local equipped = false
local ammo = script.Parent.Ammo
local mag = 15
local clipSize = script.Parent.Ammo.Value
local reloadTime = 2.19
local gui = script.Ammo
script.Parent.Equipped:Connect(function()
local arms = game.ReplicatedStorage.M4A1:Clone()
userinputservice.MouseIconEnabled = false
arms.Parent = workspace
local walkanim = arms.AnimationController:LoadAnimation(arms.Walk)
local idleAnim = arms.AnimationController:LoadAnimation(arms.Idle)
idleAnim:Play() -- TO make sure the idle animation is ready to play after the equip animation
local equiptAnim = arms.AnimationController:LoadAnimation(arms.Equip)
local runAnim = arms.AnimationController:LoadAnimation(arms.Run)
local shootAnim = arms.AnimationController:LoadAnimation(arms.Semi)
local reloadAnim = arms.AnimationController:LoadAnimation(arms.Reload)
--local aimAnim = arms.AnimationController:LoadAnimation(arms.Aiming)
equipped = true
equiptAnim:Play()
arms.Parent = workspace
main.Transparency = 1
if equipped then
gui = script.Ammo:Clone()
gui.Parent = player.PlayerGui
end
local function RefreshGui()
gui.Frame.Ammo.Text = clipSize
gui.Frame.Mag.Text = script.Parent.MaxAmmo.Value
end
wait(0.1)-- the time for the arms to load or clone.
RefreshGui()
local SwayEffect = 0.75
local SwayCFrame = CFrame.new()
local LastCameraCFrame = CFrame.new()
runService.RenderStepped:Connect(function() -- this makes the arms more realistic when you rotate the camera.
local Rotation = workspace.CurrentCamera.CFrame:ToObjectSpace(LastCameraCFrame)
local X,Y,Z = Rotation:ToOrientation()
SwayCFrame = SwayCFrame:Lerp(CFrame.Angles(math.sin(X) * SwayEffect, math.sin(Y) * SwayEffect, 0), 0.1)
LastCameraCFrame = workspace.CurrentCamera.CFrame
if arms.PrimaryPart then
arms:SetPrimaryPartCFrame(cam.CFrame *SwayCFrame * CFrame.new(0,-0.5,-1))
end
end)
humanoid.Running:Connect(function(Speed)
if not walkf then
walkf = true
if Speed > 0 then
print("20202")
walkanim.Looped = true
-- if aimAnim.IsPlaying == true then
-- aimAnim:Play()
else
walkanim:Play()
end
walkf = false
else
print("232323")
walkanim:Stop()
walkf = false
end
--end
end)
--running
local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://6466290860'
RAnimation = humanoid:LoadAnimation(RunAnimation)
Running = false
local walk = char.HumanoidRootPart:FindFirstChild("Running")
walk.SoundId = "rbxassetid://4416041299"
walk.PlaybackSpeed = 1
walk.Volume = 0.8
local jump = char.HumanoidRootPart:FindFirstChild("Jumping")
jump.SoundId = "rbxassetid://379482972"
jump.PlaybackSpeed = 1
jump.Volume = 0.5
function Handler(BindName, InputState)
if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then
Running = true
humanoid.WalkSpeed = 15
walk.PlaybackSpeed = 1.45
walk.Volume = 1.3
elseif InputState == Enum.UserInputState.End and BindName == 'RunBind' then
Running = false
if RAnimation.IsPlaying then
RAnimation:Stop()
runAnim:Stop()
end
walkf = false
humanoid.WalkSpeed = 8
walk.PlaybackSpeed = 1
walk.Volume = 0.8
runAnim:Stop()
end
end
humanoid.Running:connect(function(Speed)
if Speed >= 6 and Running and not RAnimation.IsPlaying then
walkf = true
RAnimation:Play()
runAnim:Play()
humanoid.WalkSpeed = 15 -- change your custom run speed
walk.PlaybackSpeed = 1.45-- custom speed for walk sound
walk.Volume = 1.3-- default volume for walk sound
walkanim:Stop()
elseif Speed >= 6 and not Running and RAnimation.IsPlaying then
walkf = false
RAnimation:Stop()
humanoid.WalkSpeed = 8 -- default speed
walk.PlaybackSpeed = 1 -- default speed for walk sound
walk.Volume = 0.8 -- default volume for walk sound
runAnim:Stop()
elseif Speed < 6 and RAnimation.IsPlaying then
walkf = false
RAnimation:Stop()
humanoid.WalkSpeed = 8 -- default speed
walk.PlaybackSpeed = 1 -- default speed for walk sound
walk.Volume = 0.8 -- default volume for walk sound
runAnim:Stop()
end
end)
humanoid.Changed:connect(function()
if humanoid.Jump and RAnimation.IsPlaying then
RAnimation:Stop()
walk.Volume = 0.8-- default volume for walk sound
runAnim:Stop()
idleAnim:Play()
end
end)
local reloading = false
local canshoot = true
local shootpart = arms.Gun.Handle.MuzzleFlash
local cursor = player:GetMouse()
local reloadTime = 2.19
local function Shoot()
script.Parent.OnShot:FireServer(cursor.Hit.Position, shootpart.Position)
-- if aimAnim.IsPlaying == true and mouse.Button2Down then
-- script.Parent.Handle.Shot:Play()
ammo.Value = ammo.Value - 1
RefreshGui()
-- aimAnim:Play()
--elseif aimAnim.Stopped == true then
shootAnim:Play()
-- ammo = ammo -1
script.Parent.Handle.Shot:Play()
RefreshGui()
--end
--[[script.Parent.OnShot:FireServer(cursor.Hit.Position, shootpart.Position)
shootAnim:Play()
ammo = ammo - 1
script.Parent.Handle.Shot:Play()
RefreshGui()
print("Shot")]]--
end
local function reload()
reloadAnim:Play()
if script.Parent.MaxAmmo.Value - (clipSize - script.Parent.Ammo.Value) then
script.Parent.MaxAmmo.Value = script.Parent.MaxAmmo.Value - (clipSize - script.Parent.Ammo.Value)
script.Parent.Ammo.Value = clipSize
else
script.Parent.Ammo.Value = script.Parent.Ammo.Value + script.Parent.MaxAmmo.Value
script.Parent.MaxAmmo.Value = 0
end
RefreshGui()
print("Reloading")
end
script.Parent.Activated:Connect(function()
if canshoot and not reloading then
if script.Parent.Ammo.Value <= script.Parent.MaxAmmo.Value and script.Parent.Ammo.Value > 0 then
canshoot = false
Shoot()
for i,v in pairs (shootpart:GetChildren()) do
if v:IsA("SpotLight") or v:IsA("ParticleEmitter") then
v.Enabled = true
print("Shot")
wait(0.05)
v.Enabled = false
end
end
wait(0.3)
canshoot = true
elseif ammo == 0 then
reload()
reloading = true
canshoot = false
wait(reloadTime)
reloading = false
canshoot = true
end
end
end)
userinputservice.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.R then
if script.Parent.Ammo.Value < script.Parent.MaxAmmo.Value then
if canshoot and not reloading then
canshoot = false
reloading = true
reload()
wait(reloadTime)
canshoot = true
reloading = false
end
else
return
end
end
end)
local function AimIn()
idleAnim:Stop()
--aimAnim:Play()
game.Workspace.Camera.FieldOfView = 50
end
local function AimOut()
--aimAnim:Stop()
idleAnim:Play()
game.Workspace.Camera.FieldOfView = 70
end
mouse.Button2Down:Connect(function()
local char = player.Character
local equippedTool = char:FindFirstChild("M4A1")
if equippedTool then
print("equipped tool")
AimIn()
else
print("No Tool Equipped")
end
end)
mouse.Button2Up:Connect(function()
local char = player.Character
local equippedTool = char:FindFirstChild("M4A1")
if equippedTool then
print("equipped tool")
AimOut()
else
print("No Tool Equipped")
end
end)
game:GetService('ContextActionService'):BindAction('RunBind', Handler, true, Enum.KeyCode.LeftShift)
script.Parent.Unequipped:Connect(function()
userinputservice.MouseIconEnabled = true
equipped = false
if not equipped then
gui:Destroy()
end
arms:Destroy()
end)
end)
It seems like the issue with your gun reloading only once could be related to how you’re managing the ammo and clip size values.
Based on the code you provided, it seems like you’re initializing clipSize
to be equal to the current value of script.Parent.Ammo
, which I assume represents the number of bullets in the clip. However, you’re never updating the clipSize
value after each reload. Instead, you’re updating the Ammo
value to represent the total number of bullets the player has left.
To fix this, you could update the clipSize
value whenever the player reloads their gun. One way to do this is to subtract the current clip size from the total ammo, and then set clipSize
to be the minimum of that result and the maximum clip size (i.e., 30 in your case). Here’s an example of how you could modify your code to achieve this:
local function reload()
-- Calculate the number of bullets to add to the clip
local bulletsToAdd = math.min(script.Parent.MaxAmmo.Value, 30) - clipSize
clipSize = clipSize + bulletsToAdd
script.Parent.Ammo.Value = script.Parent.Ammo.Value - bulletsToAdd
gui.Frame.Ammo.Text = clipSize
gui.Frame.Mag.Text = script.Parent.MaxAmmo.Value - clipSize
end
This function would update the clipSize
value and the ammo display in the GUI whenever the player reloads. Note that you’d need to call this function whenever the player presses the reload button (or uses some other method to reload their gun).
You’d also need to make sure that the player can’t shoot their gun if the clipSize
value is equal to zero, and that the clipSize
value is properly initialized when the player first equips their gun. You may also want to update the GUI to display the current ammo count whenever the player shoots their gun.
Comqts
(Connor)
March 8, 2023, 2:25am
#52
Looking at this code, I changed it by adding the part you added. The GUI has started to work; however, when I reload the gun, the output shows the “print(script.Parent.Ammo.Value)” and that output isn’t equal to the ammo value its self. I checked that in the properties. No easy way to explain it.
1 Like