Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Respawn Script won't put the player on a spawn?

So I am making a game loop and in this game loop, it enables a separate script to teleport any players that died to a random spawn however it will only load the character and nothing else not even an error. (Note: I am using an object value to tell the script what the map object is.)

ReplicatedStorage = game:GetService("ReplicatedStorage")
GameOn = ReplicatedStorage.Game
Maps = ReplicatedStorage.Map -- Get Map name from Object Value

game.CharacterAdded:Connect(function(char)
	repeat wait() until char:WaitForChild("Humanoid")
	char.Humanoid.Died:Connect(function() -- Making sure that this happens only when death
		if GameOn.Value == false then
			char:LoadCharacter() -- Loading the character
		else
			wait(3)
			char:LoadCharacter() -- Loading the character
		end
		local Spawns = Maps.Value:FindFirstChild('Spawns'):GetChildren() -- Get spawns
		local RandomSpawn = Spawns[math.random(1, #Spawns)] -- Pick a Random Spawn
		if char:FindFirstChild('Humanoid') and char then
			char.HumanoidRootPart.CFrame = RandomSpawn.CFrame -- Put player on picked spawn
		end
	end)
end)

6 comments

0

Whats the error code and what line is it?

0

there is no error it just does that

0

I just finished reading your code. I dont know what you were thinking, this is confusing to me, like why are you even using a for loop with the value name Players and not use it? Why even have that for loop at all? Its basically spawning the player multiple times it seems

0

Do you understand your own code,?

0

Before i help you, please fix your code, like dont use repeat until for checking for an instance, use waitforchild, char:WaitForChild"Humanoid", this way, it doesnt execute code until its loaded

0

kinda but not really my understanding of code is pretty limited and even with that I can understand what half of this is supposed to do but some of the code maybe some useless garbage that I am not noticing.

1 answer

✓ Accepted answer
RS = game:GetService("ReplicatedStorage")
GameOn = RS:WaitForChild"Game"
Maps = RS:WaitForChild"Map"

game.Players.PlayerAdded:Connect(function(plr)

        chr = plr.Character
        hum = chr:WaitForChild"Humanoid"
        hum.Died:Connect(function()

            if GameOn.Value == false then
                plr:LoadCharacter() -- Loading the character
            else
                wait(3)
                plr:LoadCharacter() -- Loading the character
            end
            local Spawns = Maps.Value:FindFirstChild('Spawns'):GetChildren() -- Get spawns
            
                local RandomSpawn = Spawns[math.random(1, #Spawns)] -- Pick a Random Spawn
                if chr and hum and chr:FindFirstChild"HumanoidRootPart" then
                    chr.HumanoidRootPart.CFrame = RandomSpawn.CFrame -- Put player on picked spawn
                end

        end)

end)

Ok so I didn't make any changes to the function I only changed the way this was written, I did stuff like use waitforchild for compact code, shorter variable names, and no character added because this runs EVERY time the character respawns making it stack and execute code multiple times which can slow down game and just is dangerous if you use waits or even random or variables, basically bad, I made it so it should still run every time the character dies since it declares that every time I think. Anyways try this, if it doesn't work, then just start debugging. You can debug code by using code watching or simple put prints in every line of code to see what doesn't print, meaning the code before that print must be what didn't work, if it all does work but not the way you want, then just figure out how to fix ur code.

Hope this helps :3

5 comments

0

I would like to update that I did update my code at the same time as when you uploaded this I will still test this but just letting you know

0

Tell me what happens or if it fails. Most importantly put a wait in hum.died and die twice, tell me if it prints twice which means it finds the character every time

0

and also to note chr = plr.Character should be local chr = plr.Character or plr.CharacterAdded:wait() to give time for the character to load and add local to hum = chr:WaitForChild"Humanoid"

0

Even if this doesn't help much with my problem I will still accept this because I think it would be better to restart this question with updated code. Sorry if this annoys you.

0

-.-

This discussion is preserved as it was. New answers, comments, and votes are closed.