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

What is the best way to reload a gun from an ammo table?

Asked by 9 years ago

How can I make it so that when a player presses shift near an ammo table, it reloads their gun for them... I'm not sure how to check if an ammo table is within a certain range, especially when there are multiple ammo tables in workspace, with the same name. Would I use the magnitude between the player and the ammo table?

1
magnitude Goulstem 8144 — 9y
0
but how can I find the closest ammo box and see if that one is in range, they are all called the same thing... dragonkeeper467 453 — 9y
1
Yes, use magnitude, but not with the Player and the table. Use the CHARACTER'S torso and the table. A Player contains nothing physical. Now, go show us an attempt and come back if you have trouble Perci1 4988 — 9y
0
oh, so you mean, cycle through all the ammo tables in workspace and check to see if any of them are in range of the player? dragonkeeper467 453 — 9y
View all comments (3 more)
0
the player's torso* dragonkeeper467 453 — 9y
0
That's a possibility. Perci1 4988 — 9y
0
I would create a table containing each ammo box then iterate through it, finding the closest one. Goulstem 8144 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago
function ReloadAmmo()
        local torso = script.Parent.Parent.Torso
        local list = game.Workspace:FindFirstChild("AmmoBox")
        local dist = 50
        if (torso.Position - list.Main.Position).magnitude < dist then
            torso.Anchored = true
            wait(2)
            clipleft.Value = clipsize
            clipsleft.Value = clips
            torso.Anchored = false
        end     
    end

ReloadAmmo is called when the player presses the shift key, All I need to do now to fix it is find out how to store all the ammo tables in an array and cycle through them, checking to see if any of them are in range, so that I can have multiple ammo tables in a single map. Thanks Perci and Goulstem for your help with this!

Ad

Answer this question