Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago
Edited 3 years ago

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)
0
Whats the error code and what line is it? AlexanderYar 788 — 3y
0
there is no error it just does that Icelogist 11 — 3y
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 AlexanderYar 788 — 3y
0
Do you understand your own code,? AlexanderYar 788 — 3y
View all comments (2 more)
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 AlexanderYar 788 — 3y
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. Icelogist 11 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
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

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 Icelogist 11 — 3y
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 AlexanderYar 788 — 3y
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" Icelogist 11 — 3y
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. Icelogist 11 — 3y
0
-.- AlexanderYar 788 — 3y
Ad

Answer this question