Problems when switching Player.Character

So I’m making an RPG where you summon and control another character by pressing a button. However once that happens, Local scripts become unusable when switching controlled characters, which raises tons of problems. I tried looking around for solutions but they tend to be unrelated or unusable. Any ideas on how to fix this?

Make sure that the network owner of the new character is indeed the player. If not, you might not be able to move, or the movement will be delayed.

I tried to set the network ownership but it still doesn’t seem to make local scripts work. I checked it using a simple click detection. Here’s the server script for switching characters.

PS: Movement is okay, just the local scripts

local repstore = game:GetService("ReplicatedStorage")
local events = repstore:WaitForChild("Events")

events.RemoteEvent.OnServerEvent:Connect(function(player, status)
	if status == "activate" then
		player.Character.Archivable = true
		character2 = player.Character:Clone()
		character2.Parent = workspace
		
		for i, parts in pairs(character2:GetDescendants()) do
			if parts:IsA("BasePart") then
				parts:SetNetworkOwner(player)
			end
		end
		
		character2.PrimaryPart.Position = player.Character.PrimaryPart.Position
		character2.Archivable = false
		player.Character.Archivable = false
		
		character2.Parent = workspace
		
		local spirit = game.ReplicatedStorage.Character1:Clone()
		spirit.Parent = workspace
		spirit.PrimaryPart.CFrame = player.Character.PrimaryPart.CFrame * CFrame.new(0,0,-5)
		
		for i, parts in pairs(spirit:GetDescendants()) do
			if parts:IsA("BasePart") then
				parts:SetNetworkOwner(player)
			end
		end
		
		player.Character = spirit
		events.RemoteEvent:FireClient(player,{CharName = spirit.Name})
		
	elseif status == "deactivate" then
		player.Character = character2
		events.RemoteEvent:FireClient(player,{CharName = player.Name})
	end
end)

Click Debug (Placed inside cloned character)

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("B R U H")
	end
end)

If I had to guess, it might be because the character doesn’t have a Player object and you are trying to use services/built-in functions that require one too early. For that, I recommend you move that stuff to StarterPlayerScripts rather than having it in a character model.

1 Like

wait if that’s the case, then does that mean that cloning the script to the character during the switch would work just fine?

Looks like cloning the scripts after changing player.character works absolutely fine, thanks for this!

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