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

Need help making a system to find the distance to a building?

My game is round based. It loads a random map from server storage into workspace every round. Inside of the map, there is a folder named "Buildings," containing multiple models called "Building." I want a system for finding the distance to the building. Please keep this in one script if possible! Here's the coding I have done so far:

-- Establish the blank variables.
local map
local distanceToBuilding

game.Players.PlayerAdded:Connect(function(player)
	-- Get the debris leaderstats value, we will use this later.
	
	local debris = player:WaitForChild("leaderstats").Debris
	-- Character is added
	
	player.CharacterAdded:Connect(function(character)
		-- Find a map
		
		if game.Workspace:FindFirstChild("Map") then
			-- Map detected
			
			print("Character added, map detected!")
			-- Give map time to load.
			
			wait(1)
			-- The map should be loaded, now define it and loop thrugh buildings.
			
			map = game.Workspace:FindFirstChild("Map")
			for i, building in pairs(map:FindFirstChild("Buildings"):GetChildren()) do
				-- This is where i need help. I need to find the distance from the character "building." Please set distanceToBuilding to this distance.
			end
		else
			-- No map.
			
			print("Character added, no map.")
		end
	end)
end)

It's all pretty well explained in the script.

EDIT: I want to eventually have it see how far the player is, ie

if distanceToBuilding >= 5 then
	print("Distance >= 5")
end

But I already have code set aside for that so don't worry ;)

1 answer

Tell me if you need more explanation. We talked about how to use GetDistanceFromPlayer() in the chat.

for i, building in pairs(map:WaitForChild("Buildings"):GetChildren()) do
	local randomPart = building[math.random(1,#(building:GetChildren())]
	local distance = player:GetDistanceFromCharacter(randomPart.Position)
end

1 comment

0

Thx

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