#reload function very exploitable

1 messages · Page 1 of 1 (latest)

rose frigate
#

by spamming reload, you can reload while shooting. I want the reloads to be cancellable, but for obvious reasons i dont want this bug.

function reload(actionName,inputState,inputObj)
    if inputState == Enum.UserInputState.Begin and equipped == true and reloading == false then
        hudTools.transparency(.5) -- hudtools is a library of functions for the hud
        shot = false -- set to true when the player shoots
        reloading = true -- set to false when the player shoots
        script.Parent.tommyServer.reload:FireServer()
        reloadTrack:Play()
        reloadTrack:AdjustWeight(1)
        wait(stats.reload)
        if shot == false and reloading == true then
            mag = stats.mag
            hudTools.updateAmmo(mag)
            hudTools.transparency(0)
        end
        reloading = false
    end
end
wraith sorrel
#

So I would split it into two different functions

Shoot
Reload

R button initiates the reload but only if the player is under max bullets while holding the gun(tool)

M1 initiates the shooting while you have ammo and your holding the gun

Then add logic where if they click R it sends to the reload function
It forces the player to wait the whole duration of the reload until it grants the ammo

Then just add some functionality to be able to cancel the reload

#

First try working on the first part then cancelling last

rose frigate
silent tulip
wraith sorrel
#
function reload(actionName, inputState, inputObj)
    if inputState == Enum.UserInputState.Begin and equipped and not reloading then -- you dont have to do == true/fase
        reloading = true 
        shot = false

        hudTools.transparency(0.5)
        script.Parent.tommyServer.reload:FireServer()
        reloadTrack:Play()
        reloadTrack:AdjustWeight(1)

        -- delay your reloading so they cant insta reload
        task.delay(stats.reload, function()
            if not shot and reloading then
                mag = stats.mag
                hudTools.updateAmmo(mag)
                hudTools.transparency(0)
                reloading = false
            end
        end)
    end
end
#

try something like this

#

just incase you dont know

task.delay is what can let you allow the player to cancel the reload
i cant test it working but the idea is it will wait however long your reload is until it fills their mag

if they shoot it will mean shot becomes true and they wont be gifted the ammo

#

you can also add

if reloading then return end

to where your shoot function is to block them from shooting while reloading

rose frigate
#

just when you spam reload you can reload while shooting

silent tulip
rose frigate
#

and im confused why its not working specifically when you spam reload which is why im here

silent tulip
rose frigate
#

thats why the reloading variable is there

civic cloak
#

mb i thought this was scripting

#

sry guys

rose frigate
#

update:

#

i fixed it by making the reload function a thread by defining the function with thread = task.spawn()

#

and whenever the player shoots it will check if the thread is nil or not (at the end of the function it sets the thread to nil)