-
What do you want to achieve? Keep it simple and clear!
So, I have this round script and it works fine, but I want the player to respawn at one of the spawnpoints if they died, rather then in the lobby. -
What is the issue? Include screenshots / videos if possible!
I’m not sure as to how I would implement this because near the start of the code, the players get teleported to one of the spawnpoints and the match carries on. But the problem is how would I be able to distinguish if they died or didn’t, and wouldn’t these spawn functions interfere with each other? -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I already tried changing the code underneath the comments " – They are dead", as the line removes the player from a table, and doing so they load back at the lobby. In fact I replaced with this with the same code that gets the players “HumanoidRootPart” and teleports them. However replacing it seems to work, but the problem is that for some reason the player repetitively respawns through each spawn which last for around 10 seconds. After an error in the output shows " ServerScriptService.RoundScript:132: attempt to index nil with ‘CFrame’
I’m not sure as to why this shows, but perhaps there is an interference with the two spawning functions, or perhaps it just keep repeating. I would assume that it keeps repeating for some time, because my output prints multiple times before eventually stopping.
Basically in short, I need help as to how to make the player respawn at one of the spawnpoints when dead. Thank you, any help would greatly appreciated!
This is my round system code,
-- Define Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 240
local cashreward = 25
local gemsreward = 5
local trophiesreward = 25
local TweenService = game:GetService("TweenService")
local statustextlabel = game.StarterGui.Status.TextLabel
-- Game Loop
while true do
Status.Value = "Waiting for enough Players"
repeat wait(1) until #game:GetService('Players'):GetPlayers() >= 1
Status.Value = "Intermission"
wait(10)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers())do
if player then
table.insert(plrs,player) -- Add each player into plrs table
end
end
wait(2)
local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
Status.Value = " Teleporting to "..ChosenMap.Name..""
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
wait(2)
-- Teleport players to the map
local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
if not SpawnPoints then
print("Spawnpoints not found!")
end
local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs)do
if player then
character = player.Character
if character then
-- Teleport them
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,5,0)
table.remove(AvailableSpawnPoints,1)
-- Give them a sword
local equipped = game.ServerStorage.PlayerData[player.Name].Equipped
if equipped.Value ~= "" then
local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
weapon.Parent = player.Backpack
else
local Sword = ServerStorage.Sword:Clone()
Sword.Parent = player.Backpack
end
local equipped2 = game.ServerStorage.PlayerData[player.Name].Equipped2
if equipped2.Value ~= "" then
local weapon2 = game.ServerStorage.Items[equipped2.Value]:Clone()
weapon2.Parent = player.Backpack
else
local Gun1 = ServerStorage.Gun1:Clone()
Gun1.Parent = player.Backpack
end
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = player.Character
else
-- There is no character
if not player then
table.remove(plrs,i)
end
end
end
end
Status.Value = "Get ready to play!"
wait(2)
Status.Value = "Be the last one standing, you only have one chance!"
wait(2)
for i = GameLength,0,-1 do
for x, player in pairs(plrs)do
if player then
character = player.Character
if not character then
-- Left the game
table.remove(plrs,x)
else
if character:FindFirstChild("GameTag")then
-- They are still alive
print(player.Name.." is still in the game!")
else
-- They are dead
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[2].CFrame + Vector3.new(0,5,0)
table.remove(AvailableSpawnPoints,2)
print(player.Name.." has been removed!")
end
end
else
print(player.Name.." has been removed!")
end
end
Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"
if plrs[1].leaderstats.Kills.Value >= 30 then
-- Last person standing
Status.Value = "The winner is "..plrs[1].Name
plrs[1].leaderstats.Coins.Value = plrs[1].leaderstats.Coins.Value + cashreward
plrs[1].leaderstats.Gems.Value = plrs[1].leaderstats.Gems.Value + gemsreward
plrs[1].leaderstats.Trophies.Value = plrs[1].leaderstats.Trophies.Value + trophiesreward
break
elseif #plrs == 0 then
Status.Value = "Nobody won!"
break
elseif i == 0 then
Status.Value = "Time up!"
break
end
wait(1)
end
print("End of game")
wait(2)
for i, player in pairs(game.Players:GetPlayers()) do
character = player.Character
if not character then
-- Ignore them
else
if character:FindFirstChild("GameTag") then
character.GameTag:Destroy()
end
for _, tool in pairs(player.Backpack:GetChildren()) do
if tool:FindFirstChild("Price") then
tool:Destroy()
end
end
for _, tool in pairs(character:GetChildren()) do
if tool:FindFirstChild("Price") then
tool:Destroy()
end
end
end
player:LoadCharacter()
end
ClonedMap:Destroy()
Status.Value = "Game ended"
wait(2)
end