In short, my problem is that I’m unable to respawn after dying. I’ve disabled CharacterAutoLoads and managed to spawn a custom character in which works completely fine (at line 5-9), however, when I die I simply don’t respawn. I’m not sure what’s wrong with my the code (starting at line 11), it has no errors but it simply doesn’t work.
I’m guessing this is a local script since LocalPlayer doesn’t exist on server, you can not access ServerStorage from client so alpha would be nil and PlayerAdded wouldn’t fire for local player (if it’s a local script)
Sorry, I should have clarified on this. The script isn’t local and it can access ServerStorage when requested, that whole function at line 5 works perfectly. The function at line 11 yields the problem.
It yields a problem because LocalPlayer - as the name suggests - only exists on the local client.
You must put the player.Character.Humanoid.Died event inside of the player added event and remove line 2. It should be fine then.
Characters loaded on a client via local script will not work.
You cannot access ServerStorage via local server script either.
Solution: Make sure you are using a normal server script and avoid using Player.LocalPlayer
OR
Make sure you are using a local script, avoid using the ServerStorage and use a remote event to trigger the respawn on the server.
If you are simply trying to load the player as a custom character then you might want to use StarterCharacter instead. There are a bunch of ways you can change the character.
Solution: I recommend this one I not long ago made a simple tutorial about 3 ways you can change the character.
It seems you are only referencing one player. If this is a server script only one player will load.
Solution: You have to put the CharacterDied event in the CharacteraAdded
This is one is a simple way you can use. However, I recommend you might want to use the StarterCharacter method instead.
-- Server Script
-- Singletons
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
-- Functions
local function CharacterRemoving()
task.wait(10)
Player:LoadCharacter()
--OR custom loading code
end
local function PlayerAdded(Player)
Player.CharacterRemoving:Connect(CharacterRemoving) -- CharacterRemoving is an Event trigger a few seconds after when the character dies
end
-- Events
Players.PlayerAdded:Connect(PlayerAdded)
Great feedback! Going off of what you said on problem 1, I believe taking this route will probably fix the issue I’m having. The script isn’t local (it was a normal server script w/ an error using local player), so I’d like to see what your solution is when using a local script. I’m just unsure how I’d go about this when I stop using ServerStorage to store the character, and how a remote event would replace its function.
Also, would this method work for every player on the server? I noticed on problem 3 that you said my method would only work for a single player as I’m using a server script.
If you are simply trying to load the player as a custom character then you might want to use StarterCharacter instead. There are a bunch of ways you can change the character.
I’m not having any trouble with problem 2, but yea, I know what you mean by simply loading a custom character. Respawning it is just what gets me since I’ve disabled CharacterAutoLoads.
It should look something like this: (this is NOT the full script)
local function loadCharacter(player)
player.Character = newCharacter
end
game.Players.PlayerAdded:Connect(function(player)
loadCharacter(player) -- you need to load the first character
player.Character.Humanoid.Died:Connect(function()
loadCharacter(player)
end)
end)
Didn’t cause any errors when I re-edited my code (it just did nothing at all, didn’t even clone the character model to the workspace). I just tried what you said and it seems to work a lot better!
Basically what I found was that the script actually dragged the character out of the server storage and into the workspace. I actually intended the script to clone what was in storage, then drag that to the workspace - that just didn’t happen.
I tested what you did by using a second character (beta) and it seemed to work just fine. Just need to connect the camera to the subject now.
I’ll probably simplify this further by finding a way to clone alpha then bring it to the workspace so I don’t have to keep creating new characters w/ different names. Just don’t know how to do that right now and I’m too tired for it. Thanks for the help!