#I get an error when running this script

1 messages · Page 1 of 1 (latest)

warped pasture
#

I get this error when running this script:

local avatar = workspace:WaitForChild("Avatar") -- replace with your model's name
local pos = avatar:GetPivot().Position

local Players = game:GetService("Players")

local closest

for i=1, #Players do
    local playerpos = Players[i]:GetPivot().Position
    if not closest then return end
    if closest.pos < playerpos then
        closest = {
            handle = Players[i],
            pos = playerpos
        }
    end
end

Btw im new to roblox scripting please dont hate 😢

devout bear
devout bear
warped pasture
# devout bear Not quite, the function comes from the `Players` service, not `game`, and you ne...

Hey sorry to bother but i cant make another post for 20 minutes, why is this npc floating in the air when i use the "MoveTo"?

Code:

local avatar = workspace:WaitForChild("Avatar") -- replace with your model's name
local pos = avatar:GetPivot().Position

while true do
    task.wait(1)
    local Players = game:GetService("Players"):GetPlayers()

    local closest

    for i=1, #Players do
        local playerpos = Players[i].Character:GetPivot().Position

        if not closest then
            closest = {
                handle = Players[i],
                pos = playerpos
            }
        else
            if closest.pos < playerpos then
                closest = {
                    handle = Players[i],
                    pos = playerpos
                }
            end
        end
    end
    
    avatar:MoveTo(closest.pos)
    
end
devout bear