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

Changed my script to use CFrame instead of Position, and now another script stopped working?

Asked by 4 years ago
Edited 4 years ago

Ok so there's a lot going on here, I'll try to keep it brief.

local function movePlayers()
    for i,v in pairs(plrs) do
        for x,z in pairs(tileTableTable) do
            if v.RowString.Value == "ClickRow"..x then
                for y,w in pairs(z) do
                    if v.TileString.Value == "ClickTile00"..y then
                        v.cPart.Position = w.Position + Vector3.new(0,1,0)
                    elseif v.TileString.Value == "ClickTile0"..y then
                        v.cPart.Position = w.Position + Vector3.new(0,1,0)
                    end
                end
            end
        end
    end
end

This is the original version of my script, which finds the tile selected by the player and then teleports the player to that tile. cPart is a part which the player is constantly being teleported to, so that the player is like a piece on a game board and the cPart is what is being manipulated to manipulate the movement of the player's character. In the final version, I want to animate the cPart to where instead of teleporting between tiles it jumps from tile to tile in a little arc. I began that process by writing some code to animate the X and Z components of traveling from one tile to the next. The modified script looks like this:

local function movePlayers()
    for i,v in pairs(plrs) do
        for x,z in pairs(tileTableTable) do
            if v.RowString.Value == "ClickRow"..x then
                for y,w in pairs(z) do
                    if v.TileString.Value == "ClickTile00"..y then
                        currentTileXPos = v.cPart.Position.X
                        currentTileZPos = v.cPart.Position.Z
                        newTileXPos = w.Position.X
                        newTileZPos = w.Position.Z
                        for i = 1,30 do
                            v.cPart.CFrame = CFrame.new(v.cPart.Position + Vector3.new((newTileXPos - currentTileXPos)/30, 0, (newTileZPos - currentTileZPos)/30))
                            wait()
                        end
                    elseif v.TileString.Value == "ClickTile0"..y then
                        currentTileXPos = v.cPart.Position.X
                        currentTileZPos = v.cPart.Position.Z
                        newTileXPos = w.Position.X
                        newTileZPos = w.Position.Z
                        for i = 1,30 do
                            v.cPart.CFrame = CFrame.new(v.cPart.Position + Vector3.new((newTileXPos - currentTileXPos)/30, 0, (newTileZPos - currentTileZPos)/30))
                            wait()
                        end
                    end
                end
            end
        end
    end
end

The animation works perfectly well, but a new problem has cropped up that I can't put my finger on. In another script I have a mechanism for determining which tile the player is currently on in order to insert a 2 tile radius of click detectors around the player for the player to select the next tile to move to. That script looks like this-

local function getCurrentRow()
    for i,v in pairs(clickRows) do
        if v.PrimaryPart.Position.Z == cPart.Position.Z then
            currentRow = v
        end
    end
    return currentRow
end

local function getCurrentTile()
    getCurrentRow(currentRow)
        for x,z in pairs(currentRow:GetChildren()) do
            if z.Position.X == cPart.Position.X then
                currentTile = z
            end
        end
    return currentTile
end

Before, every time the player moved to a new tile, these functions would run and find the new tile the player has moved to, and then another function would use that information to insert the click detectors in a 2 tile radius around the player. Now that I changed it to use CFrame to animate instead of teleporting with Position alone, every time the getCurrentRow() and getCurrentTile() functions are run it returns with the original tile the player started on, and does not detect where the player has moved. This constrains the player to only being able to move in a 5 x 5 area centered around the initial starting point.

Can anyone help? What's different about animating with CFrame versus simply teleporting that would cause this bug? Please let me know if more information needs to be given, thanks.

Answer this question