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)
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