Position swap script not working

I am trying to make a script that swaps the position between the player and an NPC.

Nothing happens when it runs, it does not error, it is not stopped, and no if statement prevents it, I even put a print at the end, it says it worked, I printed the result positions and they were how they are supposed to be, but nothing happens.

I’ve tried using different methods to move the player and NPC, and also tried commenting certain parts of the code to see if they were the problem, but got no results.

This is an example of the code I used.

local npc = game.Workspace.Dummy
local cframe1 = npc.HumanoidRootPart.CFrame
local cframe2 = game.Workspace.Fro_ZinOvr.HumanoidRootPart.CFrame
game.Workspace.Fro_ZinOvr.HumanoidRootPart.CFrame = cframe1
npc.HumanoidRootPart.CFrame = cframe2

Well, if this is a Server Script, your script is likely running very early, which would result in the Player not existing yet, Server Scripts do not wait for the Player to join the game.

the player in workspace is the character

where is your script
and is it server or loacal

The script is running on keybind through an event, and the Character is taken directly rather than through the player.

The script is servser-sided, located inside workspace for now.

could you show the keybind part

local UIS = game:GetService(“UserInputService”)

local char = script.Parent
local keybind = Enum.KeyCode.E
function swap(input)
if input.KeyCode == keybind then
game.Workspace.Event:FireServer()
end
end

Not really a good idea to be taking it directly from the workspace, with the Player, you can easily access it with .Character, where you can directly access it. so it doesnt really make sense.

Another thing is that instead of accessing the HumanoidRootPart to get the CFrame value, you should instead use GetPivot which is a CFrame of the model.

Also, RemoteEvent’s that fire to the Server have a Player variable.

forgot to put UIS.InputBegan:Connect(input)

1 Like

connect swap

local UIS = game:GetService(“UserInputService”)

local char = script.Parent
local keybind = Enum.KeyCode.E

function swap(input)
    if input.KeyCode == keybind then
        game.Workspace.Event:FireServer()
    end
end

UIS.InputBegan:Connect(swap)

For some reason, when I put it in another place, it works completely fine, but in the game I actually want it to work in it does nothing.

Apparently I just needed to add one wait in between getting positions and setting them.

I don’t know why this would be different in a seperate place, but whatever

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.