How do I get the script to not assign players with a specific string value a new string value?
Basically I am trying to get the script to tag a random player with a single StringValue and to check if the player already has a StringValue.
The problem I am facing now is that all players are given a new role even after already having a role.
Heres the Script in ServerScriptService:
Player = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
local SV = Instance.new("StringValue")
SV.Name = "Employee"
SV.Parent = player
end)
local ChosenCEO = Player.PlayerAdded:Connect(function(player)
if player:FindFirstChild("CEO" or "Manager") then
print("Player already has role")
return
elseif not player:FindFirstChild("CEO") then
print("No CEO FOUND WILL ADD LATER")
GiveRole.ChooseCEO(player)
print("CEO Added")
end
end)
local ChosenManager = Player.PlayerAdded:Connect(function(player)
if player:FindFirstChild(("CEO" or "Manager")) then
print("Player already has role")
return
elseif not player:FindFirstChild("Manager") then
print("No Manager FOUND WILL ADD LATER")
GiveRole.ChooseManager(player)
print("Manager added")
end
end)
module script in script:
function module.ChooseCEO(player)
local choosenCEO = game:GetService("Players"):GetPlayers()[math.random(1, #game.Players:GetPlayers())]
local SV = Instance.new("StringValue")
SV.Parent = player
SV.Name = "CEO"
return choosenCEO
end
function module.ChooseManager(player)
local choosenManager = game:GetService("Players"):GetPlayers()[math.random(1, #game.Players:GetPlayers())]
local SV = Instance.new("StringValue")
SV.Parent = player
SV.Name = "Manager"
return choosenManager
end
return module```
You’re using :FindFirstChild wrong, you should be checking the documentation before making a devforum post.
Script code:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local SV = Instance.new("StringValue")
SV.Name = "Employee"
SV.Parent = player
local playerCount = #Players:GetPlayers()
if playerCount == 1 then
GiveRole.ChooseCEO(player)
elseif playerCount == 2 then
GiveRole.ChooseManager(player)
end
end)
ModuleScript code:
function module.ChooseCEO(player)
local SV = Instance.new("StringValue")
SV.Name = "CEO"
SV.Parent = player
end
function module.ChooseManager(player)
local SV = Instance.new("StringValue")
SV.Name = "Manager"
SV.Parent = player
end
return module
You don’t even need :FindFirstChild if you program it a specific way. By the way, your current code didn’t even give the tag to a random player, I wasn’t sure if that was intentional or not, so I just made it like how you did.
It worked. The way you written the script is way more neater than mine.
Can you confirm that the way I written the module script to select a “random” player did not actually select a random player?
And according to my understanding, the way you wrote the script assigns the specific role at a specific playercount value? Which indirectly removes the necessity to check if every player has the role or another role already.
The way you did it was selecting a random player every time a new player joined. This indirectly just made it add the role to the person who just joined (because the previous players who joined already got their roles), which makes it not random anymore.
I see, the script I wrote in the module essentially loops it every time a player joins, and indirectly assigning that specific player the role. Alright! Much appreciation for the explanation and script.