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 fix this script that makes a player's GUI visible?

This script is supposed to open/close a player's GUI via a remote function, depending on how close the player is (or isn't) to a particular block. It's in a model that is bought in a tycoon game.

The script runs perfectly when there is only one of the brick present in the game. Any more and the script breaks. Besides this, Output gave me nothing.

Can someone help me? Here's the script:

exited = {}


while wait() do
	local armsPos = script.Parent.Torso.Position
	
	
	
	for _,v in pairs(game.Players:GetPlayers()) do
		
		if  v.Character ~= nil and (armsPos - v.Character.Torso.Position).magnitude <= 10 and not exited[v.Name] then
			game.ReplicatedStorage.ShowArmory:FireClient(v,true)
			exited[v.Name] = true		
		elseif v.Character ~= nil and (armsPos - v.Character.Torso.Position).magnitude > 10  then
			game.ReplicatedStorage.ShowArmory:FireClient(v,false)
			if exited[v.Name] then exited[v.Name] = nil end
		end
	end
end

If it matters, here is the section of the script, located in the GUI itself, that detects whether or not it should be visible.

game.ReplicatedStorage.ShowArmory.OnClientEvent:connect(function(val)
	gui.Enabled = val
end)

1 answer

In your for loop you have the following code:

for _,v in pairs(game.Players:GetPlayers()) do

You should have the following code:

for _,v in pairs(game.Players:GetChildren()) do

GetPlayers is not a valid function of Players, you still use GetChildren like you would all other objects.

Glad I could help!

1 comment

0

it works with either line.

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