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

How Do I Change While True Do To Something Else?

Can somebody help me? For some reason when my character walk to a different position the "Shop GUI" isn't showing up. It's a script that made a GUI show up when a player walk near to the brick but it doesn't work if I change my character position. How come? Can you guys help me?

local part = script.Parent
local Distance = 8 -- The distance in studs a player can be from the shop part and activate the shop
while true do
	wait()
	for i, v in ipairs(game.Players:GetPlayers())do
		if v.Character ~= nil then
		local Character = v.Character
		local Torso = Character:FindFirstChild('Torso')
		if Torso then
			local Magnitude = (part.Position - Torso.Position).magnitude
			if Magnitude <= Distance then
				local HasTag = v.PlayerGui:FindFirstChild('ShopGui')
				if not HasTag then
					local CreateTag = part.ShopGui:Clone()
					CreateTag.Parent = v.PlayerGui
				end
				else
			local HasGui = v.PlayerGui:FindFirstChild('ShopGui')
			if HasGui then
				HasGui:Remove()
			end
			end
		end
		end
	end
end

4 comments

0

I tested out your script with a part, a 'ShopGui' and this exact script inside of it, and it worked fine. Are you sure it's not working? Maybe you just have the script disabled?

0

If Filtering Enabled is enabled, it won't work.

0

@Warfaresh0t I thought that too originally, but I tested it with FilteringEnabled, and it worked...

0

Yeah, It's work guys. I'm sorry >w< But there's one problem though. If I use while true do, it is bad for my game? Cause I hear many developers say that it can cause lag to the game if I use while true do, is that true?

1 answer

You seem to have everything fixed, mainly because there isn't much of a problem. It is true that this might not work with Filtering Enabled, but there are a few other things I see disregarding that.

First, you're loop is way too fast. Currently, you're checking all the players' character's position about 30 times a second. This is way too much. I would suggest making your loop run every 1-2 seconds.

Second, you're using a deprecated function, Remove(). Personally, I really hate to see deprecated functions. Use Destroy().

Thirdly, and finally, I think your code would improve my knowing that checking if something is nil, often does the same thing as checking is something exists plainly.

local x = workspace:FindFirstChild("Mr. Part")

if x ~= nil then
	print("X Exists")
end

if x then
	print("X still Exists")
end
These will result in the same output.

Here's the code how I would mainly write it.

local part = script.Parent
local Distance = 8
while wait(1.5) do
	for i, v in ipairs(game.Players:GetPlayers()) do
		if v.Character then
			local Character = v.Character
			local Torso = Character:FindFirstChild('Torso')
			if Torso then
				local Magnitude = (part.Position - Torso.Position).magnitude
				if Magnitude <= Distance then
					local HasTag = v.PlayerGui:FindFirstChild('ShopGui')
					if not HasTag then
						local CreateTag = part.ShopGui:Clone()
						CreateTag.Parent = v.PlayerGui
					end
				else
					local HasGui = v.PlayerGui:FindFirstChild('ShopGui')
					if HasGui then
						HasGui:Destroy()
					end
				end
			end
		end
	end
end

Just minor changes. To make this work with FE if it isn't already, which I would advise, you would need to understand and utilize remote events.

The reason why this might be working for Filtering is because the script is creating the GUI, so the GUI can be seen by the server. If the GUI is replicated, like from StarterGui, the server wouldn't see the GUI. I'm not sure on the setup, so I can't tell for sure if that't what's happening.

Good Luck

1 comment

0

Mr. Part. Always so sneaky and hard to find.

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