Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

there seems to be an adding problem?

Asked by 5 years ago
Edited 5 years ago

For a random reason, the ammo it supposed to be adding per touch seems to add the original stored amount of ammo along with the refill amount. the ammo box script and gun script is here:

--ammobox script
function onTouched(hit) 
if (hit.Parent:FindFirstChild("Humanoid") ~= nil) then
local d = hit.Parent:GetChildren()
for i=1, #d do 
if (d[i].className == "Tool") then
print(d[i].StoredAmmo.Value)
d[i].StoredAmmo.Value = d[i].StoredAmmo.Value + d[i].RefillAmount.Value
script.Parent.AmmoLeft.Value = script.Parent.AmmoLeft.Value - d[i].RefillAmount.Value
d[i].Parent.Humanoid.Health = d[i].Parent.Humanoid.MaxHealth
script.Sound:Play()
print(d[i].StoredAmmo.Value)
script.Disabled = true
wait(1)
script.Disabled = false
end 
end
end
end 

script.Parent.Touched:connect(onTouched)

gun script

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local gunIsFiring = false
local enabled = false
local on = false
tool.Equipped:connect(function(mouse)
function onKeyPressR(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.R then
        if script.Parent.Ammo.Value == 0 and script.Parent.StoredAmmo.Value > 0 and enabled == true and gunIsFiring == false and on == true then 

        script.Parent.Ammo.Value = script.Parent.Ammo.Value + script.Parent.MaxAmmo.Value
        script.Parent.StoredAmmo.Value = script.Parent.StoredAmmo.Value - script.Parent.MaxAmmo.Value
        tool.Handle.ReloadSound:Play()
print(script.Parent.StoredAmmo.Value)
wait(1.4)   
on = false
enabled = false
    end


        end
    end

-- below here is irrelevant since it only deals with firing the gun
    mouse.Button1Down:connect(function()

        if script.Parent.Ammo.Value > 0 and enabled == false then
        gunIsFiring = true
            script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p + Vector3.new(math.random(-100,100)*0.01,0,0) - tool.Handle.CFrame.p).unit * 300)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = player.TeamColor
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false
        tool.Handle.FireSound:Play()
        local distance = (tool.Hole.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.2, 0.2, distance)
        beam.CFrame = CFrame.new(tool.Hole.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(20)


            end
        end
        end
        if script.Parent.Ammo.Value == 0 and enabled == false then
            gunIsFiring = false
            enabled = true
            on = true
    end
    end)
    game:GetService("UserInputService").InputBegan:connect(onKeyPressR)
end)


The output from the gun says the value of the stored ammo is what i want it to be. Whereas, the output from the ammo box says the original ammo amount + the refill amount I want. If there is something unclear, comment what is unclear before down voting.

0
first off, is it indented properly in your studio sheepposu 561 — 5y
0
Its because of the limited space on the post. EliteRayGawnX2 124 — 5y

Answer this question