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

How would I fix this script that moves players to a specific area in the map?

Asked by 10 years ago
Positions={CFrame.new(135.051, 40.7, -27.949),CFrame.new(135.051, 40.7, -42.949),CFrame.new(135.051, 40.7, -61.949)}

MovePlayer=function(player)
for p=1,#Positions do
player.Character.Torso.CFrame=Positions[p]
end

PlayerEnter=function(player)
    -- Bunch of irrelevant functions here
    player.Changed:connect(function(prop)
        if (prop=="Character") then
            MovePlayer(player)
        end
    end)
end

It's not moving the player's torso. Also, is there any way I could make the points random? I feel like if the script did work, Positions[p] would just set the move point to a same position each time.

Thank you in advance!

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

First, fix the blatant style problems.

Use standard function declaration syntax:

function name()
    -- body
end

Tab your code, and put spaces around operators! It makes it easier to read! Remove clutter!

function MovePlayer(player)
    for p = 1, #Positions do
        player.Character.Torso.CFrame = Positions[p]
    end
-- FUNCTION MISSING END

Use generic for loops:

function MovePlayer(player)
    for _, position in ipairs(Positions) do
        player.Character.Torso.CFrame = position
    end
end

Use the standard events. There's a CharacterAdded event -- don't use Changed.

function PlayerEnter(player)
    -- Bunch of irrelevant functions here
    player.CharacterAdded:connect(MovePlayer)
end

Now, this should work. However, MovePlayer will essentially only care about the last thing in the list:

n = 5
n = 4
n = 9
n = 3

Does the same thing as simply

n = 3

Which is all the loop is doing. If you were to add a wait, you would see the player appear at each point, then move to the next.

I'm guessing you want to pick a random point from the list.

function MovePlayer(character)
    character:WaitForChild("Torso").CFrame =
        Positions[math.random(#Positions)]
end
0
Ahhh! I'm sorry about the formatting, I was in a hurry to type it (I typed it directly on here, no copy&pasting so I forgot all about those tabs haha). I appreciate the tip. And thank you so much for the fix! Devotional 210 — 10y
Ad

Answer this question