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

how do I get the value of a item inside a table?

how would I get the value of the actual gun and not just the number?

local guns = game.Lighting.Folder:GetChildren()



script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		if hit.Parent.Humanoid.Health > 0 then
			local value = math.random(1,#guns)
			value.Parent = hit.Parent
		end
	end
end)

2 answers

✓ Accepted answer

Like this guns[value], GetChildren() returns a table, and you index a table by doing [] on it

Try it like this instead:

local guns = game.Lighting.Folder:GetChildren()
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		if hit.Parent.Humanoid.Health > 0 then
			local Gun = guns[math.random(1,#guns)]
			local copy = Gun:Clone()
			copy.Parent = game.Players:GetPlayerFromCharacter(hit.Parent).Backpack
		end
	end
end)

So what we did here was simple, on line 5 we get the gun from the table using the random value, then we get a copy of the gun so we don't delete the one in lighting, then we add it to the players backpack

Let me know if you have any issues ~nevan

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