So I have a ChooseSpawnOnDeath system kind of like Jailbreak’s. Whenever you die, a GUI appears and asks you to choose a spawn point. That works. If you do not choose a spawn point, the game will choose a random spawn point for you. That does not work. The countdown label reaches 5 and then respawns the character at the main spawn point, and not the points in the Locations folder in Workspace.
Hierarchy:
Server:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
Players.PlayerAdded:Connect(function(Player)
local SpawnChosen = Instance.new("NumberValue")
SpawnChosen.Name = "SpawnChosen"
SpawnChosen.Value = 0
SpawnChosen.Parent = Player
Player.CharacterAppearanceLoaded:Connect(function(Character)
if SpawnChosen.Value ~= 0 then
repeat task.wait() until Character:FindFirstChild("HumanoidRootPart")
if Character:FindFirstChild("HumanoidRootPart") then
Character.HumanoidRootPart.CFrame = workspace.Locations[tostring(SpawnChosen.Value)].CFrame
end
SpawnChosen.Value = 0
end
Character.Humanoid.Died:Connect(function()
ReplicatedStorage.ChooseSpawnOnDeathEvent:FireClient(Player, "Died")
end)
end)
Player:LoadCharacter()
end)
ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
if not Player then return end
if not Argument then return end
if Player and Argument == "Respawn" then
Player:LoadCharacter()
elseif Player and Argument == "Chosen" then
Player.SpawnChosen.Value = tonumber(Number)
Player:LoadCharacter()
end
end)
Client:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Choose = script.Parent.Choose
local Start = script.Parent.Choose.Start
local FirstCountdown = script.Parent.FirstCountdown
local Frame = script.Parent.Frame
local SecondCountdown = Frame.SecondCountdown
local CountNumber
local Continue = false
local Success = false
ReplicatedStorage.ChooseSpawnOnDeathEvent.OnClientEvent:Connect(function(Argument)
if not Argument then return end
if Argument == "Died" then
Choose.Visible = true
FirstCountdown.Visible = true
CountNumber = 5
repeat
CountNumber -= 1
FirstCountdown.Text = CountNumber
task.wait(1)
until CountNumber <= 0 or Continue == true
if CountNumber <= 0 then
ReplicatedStorage.ChooseSpawnOnDeathEvent:FireServer("Respawn")
end
end
end)
Start.MouseButton1Click:Connect(function()
Continue = true
Choose.Visible = false
FirstCountdown.Visible = false
Frame.Visible = true
CountNumber = 10
repeat
CountNumber -= 1
SecondCountdown.Text = "AUTO RESPAWN IN "..CountNumber.."..."
task.wait(1)
until CountNumber <= 0 or Success == true
if CountNumber <= 0 then
ReplicatedStorage.ChooseSpawnOnDeathEvent:FireServer("Respawn")
end
end)
for i, v in ipairs(Frame:GetChildren()) do
if v.Name == "TextButton" then
v.MouseButton1Click:Connect(function()
Frame.Visible = false
if v:FindFirstChildOfClass("NumberValue") then
ReplicatedStorage.ChooseSpawnOnDeathEvent:FireServer("Choose", v:FindFirstChildOfClass("NumberValue").Value)
else
ReplicatedStorage.ChooseSpawnOnDeathEvent:FireServer("Respawn")
end
end)
end
end
If you think you know what went wrong, please let me know. Thank you and have a wonderful day!
ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
if not Player then return end
if not Argument then return end
if Player and Argument == "Respawn" then
Player:LoadCharacter()
elseif Player and Argument == "Chosen" then
Player.SpawnChosen.Value = tonumber(Number)
Player:LoadCharacter()
end
end)
For this block it seems “Chosen” sets SpawnChosen.Value, but “Respawn” does not so it will stay the same. Try setting SpawnChosen.Value to a random value before loading the character. Maybe this will work for you?
local maximum_spawn_locations = #workspace.Locations:GetChildren() -- may be incorrect; place your actual number here if different
Player.SpawnChosen.Value = math.floor(math.random(0, maximum_spawn_locations))
Player:LoadCharacter()
ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
if not Player then return end
if not Argument then return end
if Player and Argument == "Respawn" then
Player:LoadCharacter()
elseif Player and Argument == "Chosen" then
local MaxSpawnLocations = #workspace:WaitForChild("Locations"):GetChildren()
Player.SpawnChosen.Value = math.floor(math.random(0, MaxSpawnLocations))
Player:LoadCharacter()
end
end)
ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
if not Player then return end
if not Argument then return end
if Player and Argument == "Respawn" then
local MaxSpawnLocations = #workspace:WaitForChild("Locations"):GetChildren()
Player.SpawnChosen.Value = math.floor(math.random(0, MaxSpawnLocations))
Player:LoadCharacter()
elseif Player and Argument == "Chosen" then
Player.SpawnChosen.Value = tonumber(Number)
Player:LoadCharacter()
end
end)
first of all, instead of the repeat wait : findfirstchild. its better to use waitforchild, this yields on itself.
instead of appearance loaded, you could use CharacterAdded.
instead of setting the players humanoidrootpart position it would be better to use the character:PivotTo(cframe)
and since it always chooses the main spawnpoint, are you useing the spawnpoints that come with roblox? it might be that roblox’s spawn script is overtaking your script. add a task.wait() before you set the players character position.
This is the script with the changes now. Did I do everything right?
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
Players.PlayerAdded:Connect(function(Player)
local SpawnChosen = Instance.new("NumberValue")
SpawnChosen.Name = "SpawnChosen"
SpawnChosen.Value = 0
SpawnChosen.Parent = Player
Player.CharacterAdded:Connect(function(Character)
if SpawnChosen.Value ~= 0 then
repeat task.wait() until Character:WaitForChild("HumanoidRootPart")
if Character:WaitForChild("HumanoidRootPart") then
task.wait(1)
Character.HumanoidRootPart:PivotTo(workspace.Locations[tostring(SpawnChosen.Value)].CFrame)
end
SpawnChosen.Value = 0
end
Character.Humanoid.Died:Connect(function()
ReplicatedStorage.ChooseSpawnOnDeathEvent:FireClient(Player, "Died")
end)
end)
Player:LoadCharacter()
end)
ReplicatedStorage.ChooseSpawnOnDeathEvent.OnServerEvent:Connect(function(Player, Argument, Number)
if not Player then return end
if not Argument then return end
if Player and Argument == "Respawn" then
local MaxSpawnLocations = #workspace:WaitForChild("Locations"):GetChildren()
Player.SpawnChosen.Value = math.floor(math.random(0, MaxSpawnLocations))
Player:LoadCharacter()
elseif Player and Argument == "Chosen" then
Player.SpawnChosen.Value = tonumber(Number)
Player:LoadCharacter()
end
end)
repeat task.wait() until Character:FindFirstChild("HumanoidRootPart")
if Character:FindFirstChild("HumanoidRootPart") then
task.wait(1)
Character:PivotTo(workspace.Locations[tostring(SpawnChosen.Value)].CFrame)
Nvm I fixed it! I disabled CharacterAutoLoads in the Players service and set the RespawnTime to 0. I also misnamed a parameter passed through and that was part of the problem!