I am trying to make a gui where if you click a button it changes your character to the one selected but i have a bug where if you die it goes back to the original character , and I do not know how to fix it
Error: ServerScriptService.Transformation:24: attempt to index nil with ‘Character’
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()
if newModel then
print("found")
end
end
player.Character:WaitForChild("Humanoid").Died(respawnPlayer)
game.ReplicatedStorage.Skins.ChangeCharacter.OnServerEvent:Connect(respawnPlayer)
Unfortunately, Humanoid.Died does not have any arguments passed through, and when calling the respawnPlayer function it gives out nil
You’ll have to provide some sanity checks first, to make sure that your variables at the start aren’t equal to nil and connect a new separate function for Humanoid.Died instead, whether or not to call that specified function especially since you’re connecting it where the possibility of player.Character could already be nil
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeCharacter = ReplicatedStorage.RemoteEvents:WaitForChild("ChangeCharacter")
local function CharacterAdded(char)
ChangeCharacter:FireServer(ReplicatedStorage.Char.Value)
end
local function PlayerAdded(player)
player.CharacterAdded:Connect(CharacterAdded)
local char = player.Character
if char then
print("ew")
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for i,v in pairs(game.Players:GetPlayers()) do
PlayerAdded(v)
end
and it works but only with prints , I made it so it will print “ew” every time the character changed , and it worked , but when I actually set the character instead of printing , it just sets it over and over again . like an while loop ? How do i fix this , and instead of respawning me oven and over again it will set the char just once?
It’d be appreciated if you could go ahead and send out the Server Script as well that could be causing that issue, but you should only be referencing the Character from within your CharacterAdded function and not in your PlayerAdded function as PlayerAdded will only fire once, which would be your default Character Avatar that you start out with
Trying to print it again wouldn’t work that way unless if you use the CharacterAdded Event