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

Distance checking script clones a gui to the player if they are close enough, but it doesn't work?

This was working perfectly fine until recently, but now it doesn't. I think I've found the issue, and that's that it doesn't find the player's character. I have no idea how to fix that because obviously every player has a character.

shopblock = script.Parent

shopdistance = 10

while true do

wait(.1)

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

		if v:FindFirstChild("Character") then

	char = v:FindFirstChild("Character")
			
			if char:FindFirstChild("Torso") then
	
				torso = char:FindFirstChild("Torso")		
				
				if (shopblock.Position-torso.Position).magnitude <= shopdistance then

					if not v.PlayerGui:FindFirstChild("raidershopgui") then

						print("Cloning "..shopblock.Name.."gui to the player")

						game.ReplicatedStorage[shopblock.Name.."gui"]:Clone().Parent = v.PlayerGui
				
					end
					
				elseif (shopblock.Position-torso.Position).magnitude > shopdistance and v.PlayerGui:FindFirstChild(shopblock.Name.."gui") then

					v.PlayerGui[shopblock.Name.."gui"]:Destroy()
					
					print("Destroyed "..shopblock.Name.."gui from the player")
						
				end
				
			end	
			
		end
	
	end
	
end

Thanks for reading.

1 answer

✓ Accepted answer

I believe your problem is that you are calling the method FindFirstChild which looks for descendants, when you actually want to check the value of the property of the player Character.

Changing v:FindFirstChild("Character") to v.Character in your if statement should fix your problem.

shopblock = script.Parent
shopdistance = 10

while true do
	for _, player in next, game.Players:GetPlayers()
		if player.Character and player.Character:FindFirstChild("Torso") then
			torso = player.Character.Torso
			distance = (shopblock.Position - torso.Position).magnitude
			if distance <= shopdistance then
				if not player.PlayerGui:FindFirstChild("raidershopgui") then
					game.ReplicatedStorage[shopblock.Name.."gui"]:Clone().Parent = v.PlayerGui
				end
			else
				if v.PlayerGui:FindFirstChild(shopblock.Name.."gui") then
					v.PlayerGui[shopblock.Name.."gui"]:Destroy()
				end
			end
		end
	end
	wait()
end

1 comment

0

Thanks. It worked!

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