So recently I've been working on something and I was doing fine but I have came to a stop because of a blockage, rendering me unable to continue.

If anyone can possibly help, it would be VERY appreciated.

Here is my current script:

--[[ Variables ]]--
local plr = game.Players.LocalPlayer
char = workspace:WaitForChild(tostring(plr.Name))

local ID = workspace:WaitForChild("Interactive_Doors")
local II = workspace:WaitForChild("Interactive_Items")

local Available = false
local item


--[[ Functions ]]--

function Use()
	if Available == true then
		print("picked up item")
		if item then
			item.Parent = plr
		end
	end
end


--[[ CONTROL BINDING ]]--
local controls = game:GetService("ContextActionService")

controls:BindAction("Interact", Use, false, Enum.KeyCode.E)

for i,v in ipairs(II:GetChildren()) do
	while true do
		if (char.Torso.Position - v.Position).magnitude <= 5 then
			Available = true
			item = v
		else
			Available = false
			item = nil
		end
		wait()
	end
end

At the bottom for the last bit of code, you can see I am using a while true do loop to continue checking to see if a player is near certain parts that are in a folder called "Interactive_Items".

I was wondering if there was a way to make this better or more efficient without using a loop, or at least a BETTER way of doing it with loops?

The reasoning of asking this is because I have two folders for two different types of interactions. But the issue I seek to solve is that when it goes into that loop, it gets stuck on those lines making it so I can't do a second ipairs loop for the other folder.

If you don't understand what I'm trying to explain, just let me know and I'll try my best to explain again.

Thanks again! :)

5 comments

0

You should never do a while true do wait() loop on the server, especially with such expensive calculations. You should first think of a different angle. Since this is a game design question you should walk us through this script's purpose.

0

This script is a localscript, not a script. It works with client.

0

The question is to simply find a way to do it better because without the while true do loop it doesn't constantly CHECK the distance between the part and player so I have to use SOME FORM of loop. However as I said, the while true do loop makes it stay on those lines not making me able to do the OTHER folder for interactive doors.

0

Oh locally is better but still not good. If you really want the loop then simply put while true do wait() on the outside, both loops on the inside

0

thats what I just did so lol I am stupid tbh