Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago

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
0
Thx NickIsANuke 217 — 5y
Ad

Answer this question