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

Help With Script Organization?

	local gunToGet = script.Parent.Parent.Parent.Parent.Spin.SpinnerFrame.Frame["18"].Gun.Value
	if game.Players.LocalPlayer.Slot1.Value ~= "Empty" then
		if game.Players.LocalPlayer.Slot2.Value ~= "Empty" then
			if game.Players.LocalPlayer.Slot3.Value ~= "Empty" then
				if game.Players.LocalPlayer.Slot4.Value ~= "Empty" then
					print("full inventory")
				elseif game.Players.LocalPlayer.Slot4.Value == "Empty" then
					game.Players.LocalPlayer.Slot4.Value = gunToGet
				end
			elseif game.Players.LocalPlayer.Slot3.Value == "Empty" then
				game.Players.LocalPlayer.Slot3.Value = gunToGet
			end
		elseif game.Players.LocalPlayer.Slot2.Value == "Empty" then
			game.Players.LocalPlayer.Slot2.Value = gunToGet
		end
	elseif game.Players.LocalPlayer.Slot1.Value == "Empty" then
		game.Players.LocalPlayer.Slot1.Value = gunToGet
	end

This script is supposed to go through each inventory space and check to see if it's empty, if it isn't it goes to the next and keep going until it reaches one that is empty so it can set that space to whatever it needs to be set to. The problem is that I have extended the inventory from 4 spaces to 50 so obviously that'll be a lot of lines to get the script to check each one... Is there any way to fix it so that it can check through each one and select the first empty one with a much more organized script?

2 answers

✓ Accepted answer

Yes there is! The answer to your question, is a for loop! A for loop will iterate (go through) a specified location, and you can decide what it does! Since ROBLOX Orders all Values alphabetically, and 'Slot1' will come before 'Slot2', then this will work great! (If not, be sure to add it in order)

The below script was not tested:

So from reading your script, it's just looking for an empty spot, so let's do that.

local gunToGet = script.Parent.Parent.Parent.Parent.Spin.SpinnerFrame.Frame["18"].Gun.Value
local num = 1
local foundspot = false
for i,v in pairs(game.Players.LocalPlayer:GetChildren()) do
	if string.sub(v.Name,1,4) == "Slot" then
		if v.Value == "Empty" then
			v.Value = tostring(gunToGet) -- Just in case
			foundspot = true
			break -- End the loop
		end
	end
end

if not foundspot then
	print('Full inventory')
end

4 comments

0

It's working alright, but for some reason it's skipping certain numbers. Specifically 9 and 13 from when I tested it.

0

@Scootakip Is the naming consistant?

0

Nope, looked back at the save data creator and noticed that "Empty" was spelled wrong for 9 and 13. Thanks.

0

@Scootakip Glad I could help

Use a loop

We'll just use a for loop to get all slots. If a slot isn't empty, we'll move on. If it is, we'll put the gun in it and stop.

local didPlace = false;
local p = game.Players.LocalPlayer;
local gunToGet = script.Parent.Parent.Parent.Parent.Spin.SpinnerFrame.Frame["18"].Gun.Value -- Gross
for i=1, 50 do
	local v = p:FindFirstChild("Slot"..tostring(i));
	if v then -- Let's not break your script if there's not 50 slots.
		if v.Value == "Empty" then
			v.Value = gunToGet; -- Give them the gun;
			didPlace = true;
			break; -- Stop looping
		end;
	end;
end;
if not didPlace then print "Full inv sorry m8" end;

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