So I am trying to make an Transformation Script work so when you click a button it will change you character from the current one to the one selected , but I can’t seem to make it work so when you die it saves the new character
Problem: When player dies the character reverts to original?
Error: ServerScriptService.Transformation:23: attempt to index nil with ‘WaitForChild’
Code:
local model,oldModel,newModel,oldCFrame,player,modellname
function respawnPlayer(plr, modelName)
player = plr
modellname = modelName
model = game.ReplicatedStorage.Skins:FindFirstChild(modelName)
print("Model from server is", model)
oldModel = plr.Character
newModel = model:Clone()
oldCFrame = oldModel:GetPrimaryPartCFrame()
plr.Character = newModel
newModel.Parent = workspace
newModel:SetPrimaryPartCFrame(oldCFrame)
oldModel:Destroy()
end
game.ReplicatedStorage.Skins.ChangeCharacter.OnServerEvent:Connect(respawnPlayer)
repeat
if newModel:WaitForChild("Humanoid").Health <= 0 then
player.Character = newModel
print("char set")
end
until player.Character == newModel
I’ve actually done something like this when I was working on an old project.
What I’ve done is used a RemoteEvent that’ll change the appearance of the player, and then either I had the gui (with the script inside) be refreshed every time the player has dies or, in that script, attach the player.CharacterAdded function. That’ll be called everytime the player spawns basically, or when the character is created within the workspace.
-- The LocalScript located inside StarterGUI (or StarterPlayer, whatever works for you)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeCharacter = ReplicatedStorage.Skins:WaitForChild("ChangeCharacter")
local Player = game.Players.LocalPlayer
Player.CharacterAdded:Connect(function(character)
ChangeCharacter:FireServer(...) -- ... is just the modelName you want to give it to.
end)
If you have a question about this script let me know, otherwise I hope this helped and good luck on your game!
Where are you running the code in? Or, maybe I’ve misunderstood how I’ve done it. If that didn’t work, how about instead fire the server like usual in the local script, and then connect to the players.CharacterAdded on the server script instead?
Something like this:
-- make sure this is running in a server-sided script too
local ReplicatedStorage = game:GetService("ReplicatedStorage")
function charRespawn(character)
oldCFrame = character:GetPrimaryPartCFrame()
player.Character = newModel
newModel.Parent = workspace
newModel:SetPrimaryPartCFrame(oldCFrame)
character:Destroy()
end
function respawnPlayer(player, modelName)
charRespawn(player.Character)
player.CharacterAdded:Connect(charRespawn)
end
ReplicatedStorage.Skins.ChangeCharacter.OnServerEvent:Connect(respawnPlayer)
Maybe the bigger question is where are you running the original script in.