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

Issue switching character CFrames

Asked by 10 years ago

I've been making something and this bug occurred. It wasn't just the script, it has to be something in the way I do this; I made a simplified version of it and then used it. The bug is that when switching the CFrames of two character's torsos, sometimes only one will move, causing them to get stuck together. I've made a video of the bug here. I know the quality is low, just focus on the bug.

The code used in the video is this:

local plr=game.Players.LocalPlayer
local char=plr.Character
local mouse=plr:GetMouse()

mouse.Button1Down:connect(function()
    if mouse.Target and mouse.Target.Parent:findFirstChild("Torso") then
        local targ=mouse.Target.Parent
        char.Torso.CFrame,targ.Torso.CFrame=targ.Torso.CFrame,char.Torso.CFrame
    end
end)

1 answer

Log in to vote
0
Answered by
AxeOfMen 434 Moderation Voter
10 years ago

Interesting bug. Are you able to duplicate it in Studio with Start Server/Start Player? I tried it out and while I did not see the behavior from your video, I did get an error. The problem was that 'char' was being set too early. I was able to resolve it by waiting for the character:

local plr=game.Players.LocalPlayer
while not plr.Character do wait() end --Added this line
local char=plr.Character
local mouse=plr:GetMouse()

mouse.Button1Down:connect(function()
    if mouse.Target and mouse.Target.Parent:findFirstChild("Torso") then
        local targ=mouse.Target.Parent
        char.Torso.CFrame,targ.Torso.CFrame=targ.Torso.CFrame,char.Torso.CFrame
    end
end)

After adding that line, I was able to swap positions with another player both offline and online. I never did see the bug you are seeing, though. I wonder if another script is interfering.

At any rate, there's nothing else wrong with the script you posted beyond waiting for plr.Character. Good luck!

Ad

Answer this question