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

My location popup script doesn't work?

Asked by 6 years ago

So I made a script that's supposed to tell the player in which they neighborhood they are after they exit the current one (basically like the city names that appear in gta games) but it doesn't work:

game.Players.LocalPlayer.CharacterAdded:Wait()
local player = game.Players.LocalPlayer
local char = game.Players.LocalPlayer.Character
local hum = char:WaitForChild("HumanoidRootPart")
local pos = hum.Position
local gui = player.PlayerGui.mainGUI

function isInRegion3(region, point)
    local relative = (point - region.CFrame.p) / region.Size
    return -0.5 <= relative.X and relative.X <= 0.5
       and -0.5 <= relative.Y and relative.Y <= 0.5
       and -0.5 <= relative.Z and relative.Z <= 0.5
end

local nbh = {}
nbh.city= 
{
    name = "city",
    coords = Region3.new(Vector3.new(-821,0,644), Vector3.new(101,1300,1546))
}

local loc = "a"
local temploc = "a"

while true do
    wait(3)
    for _, v in pairs(nbh) do
        if isInRegion3(v.coords, pos) then
            loc = v.name
        else
            loc = "notcity"
        end 
    end
    if loc ~= temploc then
    ~~make popup appear~~
        temploc = loc
    end
end

Despite the player being in the area of "city", the popup appears as "notcity", so I think the problem is in the part that detects whether or not the player is in the region.

Help?

1 answer

Log in to vote
1
Answered by 6 years ago

On line 5 you have local pos = hum.Position this will hold the HumanoidRootPart current position which is used on line 28 but never updated to hold the current player position.

You need to access the position each time you are checking the players location.

while true do
    wait(3)
    local plrPos = hum.Position -- get the player position each time
    for _, v in pairs(nbh) do
        if isInRegion3(v.coords, plrPos) then -- 
            loc = v.name
        else
            loc = "notcity"
        end 
    end
    if loc ~= temploc then
    ~~make popup appear~~
        temploc = loc
    end
end

This should fix your position issue but you might be better off just checking the x,y,z of the Vector3.

Hope this helps.

0
thanks. radusavin366 617 — 6y
Ad

Answer this question