Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

7 comments

1

magnitude

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...

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

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?

0

the player's torso*

0

That's a possibility.

0

I would create a table containing each ammo box then iterate through it, finding the closest one.

1 answer

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.