Hello!
I’m trying to make a city game, and there’d be a player leader that basically controls the town. I want the leaders to be chosen randomly, and whenever a leader dies, another person gets chosen randomly. I asked for help on the forum already, but the script wasn’t working, and the person didn’t respond.
Thanks for helping, here’s the script:
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local CurrentLeader
local LockLeaderForFirstJoin = false
local function SelectRandomLeader()
return Players:GetPlayers()[Random.new():NextInteger(1, #Players:GetPlayers())]
end
Players.PlayerAdded:Connect(function(PlayerThatJoined)
PlayerThatJoined.Team = Teams.Choosing
if not LockLeaderForFirstJoin then
LockLeaderForFirstJoin = true
CurrentLeader = PlayerThatJoined
PlayerThatJoined.Team = Teams.Leader
print(("%s is the current leader"):format(tostring(CurrentLeader)))
PlayerThatJoined.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end
PlayerThatJoined.CharacterAdded:Connect(function(CharacterThatJoined)
CharacterThatJoined.Humanoid.Died:Connect(function()
CurrentLeader.Team = Teams.Choosing
local NewLeader = SelectRandomLeader()
NewLeader.Team = Teams.Leader
print(("%s has died. %s is the new leader."):format(tostring(CurrentLeader), tostring(NewLeader)))
NewLeader.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end)
end)
end)
Are there any errors you are getting in the output and where is this script located?
It’s located in serverscriptservice, and I got multiple errors, some saying 'attempt to index nil with HumanoidRootPart, and some, no errors at all, when a leader dies, another person didn’t become leader.
The error I got the most was the attempt to index nil, I forgot to mention it when I wrote the topic.
PlayerThatJoined.Character:WaitForChild("HumanoidRootPart").Position = workspace.CivilianSpawn.Position
Put this line after Character Added.
I just put a WaitForChild and the character added event waits for the character to be added so the character is not nil.
I am not sure if this is the main problem and because I don’t have access to Roblox Studio right now I am not being able to test the script out myself either.
No like, when someone becomes leader, they automatically teleport to the spawn, while the non leader players spawn at the lobby, where they choose teams. (the lobby spawns are basically Spawnlocations.)
try this
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local CurrentLeader
local LockLeaderForFirstJoin = false
local function SelectRandomLeader()
return Players:GetPlayers()[Random.new():NextInteger(1, #Players:GetPlayers())]
end
Players.PlayerAdded:Connect(function(PlayerThatJoined)
PlayerThatJoined.Team = Teams.Choosing
if LockLeaderForFirstJoin == false then
LockLeaderForFirstJoin = true
CurrentLeader = PlayerThatJoined.Name
PlayerThatJoined.Team = Teams.Leader
print(("%s is the current leader"):format(tostring(CurrentLeader)))
PlayerThatJoined.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end
PlayerThatJoined.CharacterAdded:Connect(function(CharacterThatJoined)
CharacterThatJoined.Humanoid.Died:Connect(function()
CurrentLeader.Team = Teams.Choosing
local NewLeader = SelectRandomLeader()
NewLeader.Team = Teams.Leader
print(("%s has died. %s is the new leader."):format(tostring(CurrentLeader), tostring(NewLeader.Name)))
NewLeader.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end)
end)
end)
Still the same issue, attempt to index nil.
Can you show me a screenshot of the output?
23:19:34.484 Player1 is the current leader - Server - LeaderScript:17
23:19:34.484 ServerScriptService.LeaderScript:18: attempt to index nil with 'HumanoidRootPart' - Server - LeaderScript:18
23:19:34.484 Stack Begin - Studio
23:19:34.484 Script 'ServerScriptService.LeaderScript', Line 18 - Studio - LeaderScript:18
23:19:34.484 Stack End - Studio
try using WaitForChild()
PlayerThatJoined.Character:WaitForChild("HumanoidRootPart").Position = workspace.CivilianSpawn.Position
– line 18
Nope:
23:22:58.514 ServerScriptService.LeaderScript:18: attempt to index nil with 'WaitForChild' - Server - LeaderScript:18
23:22:58.514 Stack Begin - Studio
23:22:58.514 Script 'ServerScriptService.LeaderScript', Line 18 - Studio - LeaderScript:18
23:22:58.515 Stack End - Studio
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local CurrentLeader
local LockLeaderForFirstJoin = false
local function SelectRandomLeader()
return Players:GetPlayers()[Random.new():NextInteger(1, #Players:GetPlayers())]
end
Players.PlayerAdded:Connect(function(PlayerThatJoined)
PlayerThatJoined.Team = Teams.Choosing
repeat task.wait() until PlayerThatJoined.Character
if LockLeaderForFirstJoin == false then
LockLeaderForFirstJoin = true
CurrentLeader = PlayerThatJoined.Name
PlayerThatJoined.Team = Teams.Leader
print(("%s is the current leader"):format(tostring(CurrentLeader)))
PlayerThatJoined.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end
PlayerThatJoined.CharacterAdded:Connect(function(CharacterThatJoined)
CharacterThatJoined.Humanoid.Died:Connect(function()
CurrentLeader.Team = Teams.Choosing
local NewLeader = SelectRandomLeader()
NewLeader.Team = Teams.Leader
print(("%s has died. %s is the new leader."):format(tostring(CurrentLeader), tostring(NewLeader.Name)))
NewLeader.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end)
end)
end)
Nobody’s the leader now, whenever someone joins, nobody’s the leader, no errors too.
Here’s mine:
(I changed line 12 cause there was an error)
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local CurrentLeader
local LockLeaderForFirstJoin = false
local function SelectRandomLeader()
return Players:GetPlayers()[Random.new():NextInteger(1, #Players:GetPlayers())]
end
Players.PlayerAdded:Connect(function(PlayerThatJoined)
PlayerThatJoined.Team = Teams.Choosing
repeat task.wait() until PlayerThatJoined.Character
if LockLeaderForFirstJoin == false then
LockLeaderForFirstJoin = true
CurrentLeader = PlayerThatJoined.Name
PlayerThatJoined.Team = Teams.Leader
print(("%s is the current leader"):format(tostring(CurrentLeader)))
PlayerThatJoined.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end
PlayerThatJoined.CharacterAdded:Connect(function(CharacterThatJoined)
CharacterThatJoined.Humanoid.Died:Connect(function()
CurrentLeader.Team = Teams.Choosing
local NewLeader = SelectRandomLeader()
NewLeader.Team = Teams.Leader
print(("%s has died. %s is the new leader."):format(tostring(CurrentLeader), tostring(NewLeader.Name)))
NewLeader.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end)
end)
end)
why don’t u try teleporting the leader when the characteradded function fires?
Like this? I didn’t write this script, someone on the devforum did:
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local CurrentLeader
local LockLeaderForFirstJoin = false
local function SelectRandomLeader()
return Players:GetPlayers()[Random.new():NextInteger(1, #Players:GetPlayers())]
end
Players.PlayerAdded:Connect(function(PlayerThatJoined)
PlayerThatJoined.Team = Teams.Choosing
repeat task.wait() until PlayerThatJoined.Character
if LockLeaderForFirstJoin == false then
LockLeaderForFirstJoin = true
CurrentLeader = PlayerThatJoined.Name
PlayerThatJoined.Team = Teams.Leader
print(("%s is the current leader"):format(tostring(CurrentLeader)))
end
PlayerThatJoined.CharacterAdded:Connect(function(CharacterThatJoined)
PlayerThatJoined.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
CharacterThatJoined.Humanoid.Died:Connect(function()
CurrentLeader.Team = Teams.Choosing
local NewLeader = SelectRandomLeader()
NewLeader.Team = Teams.Leader
print(("%s has died. %s is the new leader."):format(tostring(CurrentLeader), tostring(NewLeader.Name)))
NewLeader.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
end)
end)
end)
TIP:
Here’s another way to do it:
local character = PlayerThatJoined.Character or PlayerThatJoined.CharacterAdded:Wait()
1 Like