Radius GUI Appear script bugging when 2 Or more people are ingame

Hi!, I have a radius script which checks whenever are player is near a Part then makes a GUI Visible however if there are two or more people it starts bugging out like this :
https://gyazo.com/0e784d7e9d2578208a83afdd22821f75

And when there is only one person AKA it acting normal it is like this :
https://gyazo.com/9d05272e5d2f2348d45b07e2a0e9f869

The code is

local a = false
local gui = script.Parent.chest.yes
while task.wait() do
	local distance = 10
	for _, player in ipairs(game.Players:GetPlayers()) do
		local char = player.Character or player.CharacterAdded:Wait()
		local target = char:FindFirstChild("HumanoidRootPart")
		if target then
			if (target.Position - script.Parent.Position).Magnitude <= distance and a == false then
				a = true
				gui.Visible = true
				spawn(function()
					local r = Random.new()
					for i =1,10 do
						gui.Rotation = r:NextNumber(-10, 10)
						gui.Position = UDim2.new(r:NextNumber(0.45, 0.55),0,r:NextNumber(0.4, 0.6),0)
						gui.Size = UDim2.new(0,r:NextNumber(100, 300),0,r:NextNumber(100, 200))
						task.wait()
					end
					gui.Size = UDim2.new(0,300,0,200)
					gui.Rotation = 0
					gui.Position = UDim2.new(0.5,0,0.5,0)
				end)
			elseif (target.Position - script.Parent.Position).Magnitude >= distance and a == true then
				a = false
				spawn(function()
					local r = Random.new()
					for i =1,10 do
						gui.Rotation = r:NextNumber(-10, 10)
						gui.Position = UDim2.new(r:NextNumber(0.45, 0.55),0,r:NextNumber(0.4, 0.6),0)
						gui.Size = UDim2.new(0,r:NextNumber(100, 300),0,r:NextNumber(100, 200))
						task.wait()
					end
					gui.Visible = false
					gui.Size = UDim2.new(0,300,0,200)
					gui.Rotation = 0
					gui.Position = UDim2.new(0.5,0,0.5,0)
				end)
			end
		end
	end
end

Help would be appreciated thank you

1 Like

Make sure to remove the gui when the player leaves the server.
game.Players.PlayerRemoving:connect(function(player)
gui.Visible = false
end)

why would I need this my problem is with too many people in the server what would that do?

1 Like