So I am making a script where all of the players are put into a table and one player is picked to have a role and then that player has a number value that is changed. I am trying to make it where it will call a remote event that will go to a local script that will say “You are the mafia” and tween it. But the mafia variable is not a user so it cannot be passed with a RemoteEvent (i think). Could someone help with that?
while true do
wait(3)
contestants = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player and player.Character then
local humanoid = player.Character:WaitForChild("Humanoid")
if humanoid and humanoid.Health > 0 then
table.insert(contestants, player)
end
end
end
local weightedcontestants = {}
mafia = contestants[math.random(1)]
mafia:FindFirstChild("MafiaValue").Value = 1
print("Firing Client")
wait(5)
mafiaEvent:FireClient(mafia)
print("Done")
wait(180)
Your while true loop is running forever. You need to add break in there somewhere if you want it to stop once some condition is met, or if you only want it to run a set number of times you could use the for i = 1, 20 (or any number in place of 20) pattern.
Whoops, misread. You should indent your code.
Are you missing an end at the bottom of your script?
Yep.
You are missing an end for the while loop.
Partly Fixed Code:
while true do
wait(3)
contestants = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player and player.Character then
local humanoid = player.Character:WaitForChild("Humanoid")
if humanoid and humanoid.Health > 0 then
table.insert(contestants, player)
end
end
end
local weightedcontestants = {}
mafia = contestants[math.random(1)]
mafia:FindFirstChild("MafiaValue").Value = 1
print("Firing Client")
wait(5)
mafiaEvent:FireClient(mafia)
print("Done")
wait(180)
end
Make contestants and mafia local. That way other scripts won’t have extra variables floating around.
To select a random player from the contestants table, you don’t do contestants[math.random(1)]. You would do contestants[math.random(#contestants)] - that is, generate a random number from 1 to the amount of contestants, and select that contestant.
weightedcontestants is not used and can be deleted.
Further fixed code:
while true do
wait(3)
local contestants = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player and player.Character then
local humanoid = player.Character:WaitForChild("Humanoid")
if humanoid and humanoid.Health > 0 then
table.insert(contestants, player)
end
end
end
local mafia = contestants[math.random(#contestants)]
mafia:FindFirstChild("MafiaValue").Value = 1
print("Firing Client")
wait(5)
mafiaEvent:FireClient(mafia)
print("Done")
wait(180)
end
Additionally, you might want to remove the mafia value from the previous mafia before selecting the next one. You can do that by moving mafia above the loop, and setting the value to 0 at the beginning if there was a previous member.
Code
local mafia
wait(3)
while true do
if mafia ~= nil and mafia:FindFirstChild('MafiaValue') ~= nil then
mafia.MafiaValue.Value = 0
end
local contestants = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player and player.Character then
local humanoid = player.Character:WaitForChild("Humanoid")
if humanoid and humanoid.Health > 0 then
table.insert(contestants, player)
end
end
end
mafia = contestants[math.random(#contestants)]
mafia.MafiaValue.Value = 1
print("Firing Client")
wait(5)
mafiaEvent:FireClient(mafia)
print("Done")
wait(180)
end
Also you might want to make MafiaValue a boolean value since the only value you’re storing is true or false. In the script you would then assign true/false to the Value rather than 0/1.
(sorry for so many edits, I accidentally hit tab and the forum published my post waaay before I was done with it. Should be mostly finished now, but I’m in a major rush now thanks to the error)
(also again, I should REALLY find a way to stop hitting Ctrl/Tab by accident all the time)
Nope. It says you’re attempting to call OnClientEvent.
You might have to post that script for me to know what’s going on here.
You can pass Instances through RemoteEvents so that’s not the problem. I think the problem is how you’re trying to receive data from the server. I need to see your local script to help you out.