So i made this code below, what im trying to achieve is that the player that gets chosen as the ‘Hunter’ the status value will say “You have been chosen as the hunter” and for the other players i want “You have been chosen as a Ghost”.
Whats happening currently is that first the hunter message shows, then shortly after the ghost message shows for everyone including the hunter!
Im not using a local script. This code is part of a larger round system code and i was wondering how i could achieve what i want?
Do i have to use a local script and hook it up to the round script, do i have to use events? I have no idea at the moment. Im hoping theres someone that can help me
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local PlayersTable = Players:GetPlayers()
local RandomPlayer = PlayersTable[math.random(#PlayersTable)]
local hunter = replicatedstorage.Juggernaut_Rigged
for i = 1, #PlayersTable do
local Player = PlayersTable[i]
if Player == RandomPlayer then
Player.Team = Teams["Hunters"]
status.Value = "You have been chosen as the Hunter"
local newCharacter = hunter:Clone()
newCharacter.Name = Player.Name
Player.Character = newCharacter
newCharacter.Parent = workspace
else
Player.Team = Teams["Ghosts"]
status.Value = "You have been chosen as a ghost"
end
end
You will have to use RemoteEvents. You’ll need to use FireClient to tell a specific player what they are…
The Status doesn’t work because its run on the server, you’ll need to do it locally.
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local PlayersTable = Players:GetPlayers()
local RandomPlayer = PlayersTable[math.random(#PlayersTable)]
local hunter = game:GetService("ReplicatedStorage").Juggernaut_Rigged
local roleremote = game:GetService("ReplicatedStorage").roleremote
for i = 1, #PlayersTable do
local Player = PlayersTable[i]
if Player == RandomPlayer then
Player.Team = Teams["Hunters"]
roleremote:FireClient(Player, "Hunters")
local newCharacter = hunter:Clone()
newCharacter.Name = Player.Name
Player.Character = newCharacter
newCharacter.Parent = workspace
else
Player.Team = Teams["Ghosts"]
roleremote:FireClient(Player, "Ghosts")
end
end
Local Script in StarterGUI
local roleremote = game:GetService("ReplicatedStorage").roleremote
local status = script.Parent
roleremote.OnClientEvent:Connect(function(role)
status.Text = role
end)
This will take the value that you send by remoteevent and set it as the text value to a a GUI Object, which may be what you want. This avoids the use of a value. But if you want to use the value, feel free to replace it with that. If you want me to explain remote events more, just let me know! Hope this works for you.
The status value should be inside of each player, instead of being global. However, if you want a system to where players can’t know who is the hunter until they attack (like in Murder Mystery 2), it should be done with RemoteEvents like @ValtryekRBLX says. But if everybody always sees if a player is the hunter, it should not matter too much if it’s in each player.
local Teams = game:GetService("Teams")
local HunterCharacter = game.ReplicatedStorage.Juggernaut_Rigged
local UsersInRound = game.Players:GetPlayers()
local CurrentHunter = UsersInRound[math.random(1, #UsersInRound)]
for _, player in pairs(UsersInRound) do
local status = player.CurrentStatus -- ⚠ Add this stringvalue in the player instead of it being global
if player == CurrentHunter then
player.Team = Teams.Hunters
status.Value = "Hunter" -- You should finish the message on the client instead of putting the full message here. That allows for creative freedom.
local newchar = HunterCharacter:Clone()
newchar.Name = player.Name
newchar:PivotTo(player.Character:GetPivot()) -- Or move the character to some spawn or something.
newchar.Parent = workspace
player.Character = newchar
else
player.Team = Teams.Ghosts
status.Value = "Ghost"
end
end
-- The last hunter should be respawned using :LoadCharacter()
Basically, using RemoteEvents will privately tell each player what their role is, because with a value indicating the user’s team in their player object, exploiters will see that and they can know everybodies role. But this power is lost if everybody can see who the hunter is from the start, like in this game. That also means the ‘Team’ property is also an indicator for their role. Keep this in mind!
Yes, you can do this. Just change the local script to this:
local roleremote = game:GetService("ReplicatedStorage").roleremote
local status = script.Parent
roleremote.OnServerEvent:Connect(function(role)
if role == "Hunters" then
status.Text = "You are now a Hunter"
else
status.Text = "You are now a ghost!"
end
end)