On your client side, you can use :FindFirstChildWhichIsA
to get your model:
selectButton.MouseButton1Click:Connect(function()
local model = location:FindFirstChildWhichIsA("Model")
assert(model, "model does not exist for this location!")
respawnEvent:FireServer(model.Name)
end)
On your server-side, I think you are making a misconception of what player.Character
and player:LoadCharacter
are supposed to do. .Character
gives you the current character, and :LoadCharacter
only respawns the character, nothing else is done.
The default control and camera scripts are not dependent on character changes like this, I would only be potentially worried about network ownership.
You should be able to just set player.Character
directly on the server without having to use :LoadCharacter
, but I am rather unfamiliar with the mechanics of loading a character through setting a property like this. I would believe it to be like so:
local function respawnPlayer(plr, modelName)
local model = replicatedstorage.Characters:FindFirstChild(modelName)
print("Model from server is", model)
local oldModel = plr.Character
local newModel = model:Clone()
local oldCFrame = oldModel:GetPrimaryPartCFrame()
plr.Character = newModel
newModel.Parent = workspace -- [1] this follows current character loading behavior
newModel:SetPrimaryPartCFrame(oldCFrame)
oldModel:Destroy()
end
respawnEvent.OnServerEvent:Connect(respawnPlayer)