Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I change the parent of local player's character?

I made a script that changes all the characters of the players (models in workspace) to another model so the player moves the dirrection the model is moving. That works perfectly fine now I want the player's model to move from that model back to workspace when the player presses space but it doesn't work. I tried it both in game and in roblox studio solo. It is a local script located in the player starter script.

local localplayername = game.Players.LocalPlayer.Name
local m = game.Players.LocalPlayer:GetMouse()
db = true
m.KeyDown:connect(function(k)
	if k == "32" then
		if db == true then
			for _, descendant in 	pairs(game.Workspace.FlightModel:GetDescendants())do
				if descendant:IsA("Model") then
					if descendant.Name == localplayername then
						descendant.Parent = game.Workspace
					end
				end
			end
		end
	end
end)

1 comment

0

KeyDown is deprecated, do not use it in new work.

2 answers

You have a few problems in your code.

First, KeyDown and :connect are both deprecated, so I highly recommend to not use them. Instead, you can use the UserInputService to detect the player's key inputs. And just use :Connect

Secondly, you are changing the parent of the player in a local script. This would only show on client side. A way to fix this would be to use RemoteEvents to tell the server to change the player's parent.

Lastly, I don't know if this is correct, but I believe that :GetDescendants is the same thing as :GetChildren, correct? If not, please correct me.

Hopefully this helps! If you have any questions, please ask.

An example of using UIS with a RF as Creeperman said...

Local Script

game:GetService("UserInputService").InputBegan:connect(function(key)
    if key.KeyCode == Enum.KeyCode.F then
        script.Parent.RemoteEvent:FireServer() -- RemoteFunction can be anywhere, as long as the server script knows where it is.
    end
end)

Script

script.Parent.RemoteEvent.OnServerEvent:connect(function(plr)
    --The first argument in an OnServerEvent is the player who fired the RemoteEvent.

    --Code here, this will be in a server script so everyone see's the change.
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.