You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
What is the issue? Include screenshots / videos if possible!
Why it does not return he value I asked for
When I select the ‘‘Prototype’’ it selects him but after when It checks it responds with nil (and since it reponds with nil it chooses a random characther)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried changing it mutliple times the system but it does not return the value
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
function getVotes()
local voteCount = {}
local winningScore = 0
local winningVote
for choice, data in votes do
voteCount[choice] = 0
for i, v in data do
voteCount[choice] += 1
end
if voteCount[choice] > winningScore then
winningVote = choice
winningScore = voteCount[choice]
print(choice)
end
end
game.ReplicatedStorage.Events.Choose.Vote:FireAllClients(voteCount)
return winningVote
end
local function getMobs()
votes = {}
for i, map in game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs:GetChildren() do
votes[map.Name] = {}
end
local mobName = getVotes()
warn(mobName)
if mobName then
choose(mobName)
else
local mobs = game.ReplicatedStorage.Values.ChoosedMobs.RandomMobs:GetChildren()
local rndMob = mobs[math.random(1, #mobs)]
choose(rndMob.Name)
end
return
end
The script returns nothing becuase the tables are empty.
Your check:
if voteCount[choice] > winningScore then
checks if it’s greater than zero, but since the table has nothing in it (0) it’s not greater than the winning score, so the winning score is never assigned a value (nil).
As well as this, I’ve changed some of your code to make it more efficient
for choice, data in votes do
voteCount[choice] = #data
if voteCount[choice] > winningScore then
winningVote = choice
winningScore = voteCount[choice]
print(choice)
end
end
Sorry if I wasn’t clear in my question but what im trying to achieve is a voting system and the problem Is it detects which characther I chose
if voteCount[choice] > winningScore then
winningVote = choice
winningScore = voteCount[choice]
print(choice)
end
Its just that It returns nil
It detects the winner
local function getMobs()
votes = {}
for i, map in game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs:GetChildren() do
votes[map.Name] = {}
end
local mobName = getVotes()
warn(mobName)
if mobName then
choose(mobName)
else
local mobs = game.ReplicatedStorage.Values.ChoosedMobs.RandomMobs:GetChildren()
local rndMob = mobs[math.random(1, #mobs)]
choose(rndMob.Name)
end
return
The error is really weird lol Idk if its something stopping the return in the ‘‘getVotes’’ function Ill send the full ‘‘voting script’’ maybe itll help
local votes = {}
function ProcessVote(plr,Choice)
for i, Choice in votes do
Choice[plr.UserId] = nil
end
votes[Choice][plr.UserId] = true
print(votes)
getVotes()
end
game.ReplicatedStorage.Events.Choose.AddVote.OnServerEvent:Connect(function(plr,mob)
local value = Instance.new('BoolValue')
value.Parent = game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs
value.Name = mob.Name
local image = Instance.new('StringValue')
image.Parent = value
image.Value = mob.Image.Value
image.Name = 'Image'
local color = Instance.new('Color3Value')
color.Parent = value
color.Value = mob.Color.Value
color.Name = 'Color'
end)
game.ReplicatedStorage.Events.Choose.AddVoteModifer.OnServerEvent:Connect(function(plr,mob)
local value = Instance.new('BoolValue')
value.Parent = game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier
value.Name = mob.Name
local image = Instance.new('StringValue')
image.Parent = value
image.Value = mob.Image.Value
image.Name = 'Image'
local color = Instance.new('Color3Value')
color.Parent = value
color.Value = mob.Color.Value
color.Name = 'Color'
end)
game.ReplicatedStorage.Events.Choose.VoteStart.OnServerEvent:Connect(function(plr , set)
votes = {}
if set == 'Mob' then
for i, map in game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs:GetChildren() do
votes[map.Name] = {}
end
else
for i, map in game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:GetChildren() do
votes[map.Name] = {}
end
end
end)
game.ReplicatedStorage.Events.Choose.Vote.OnServerEvent:Connect(ProcessVote)
function getVotes()
local voteCount = {}
local winningScore = 0
local winningVote
for choice, data in votes do
-- print(votes)
voteCount[choice] = 0
for i, v in data do
voteCount[choice] += 1
end
if voteCount[choice] > winningScore then
winningVote = choice
winningScore = voteCount[choice]
-- Has Normal Info
print(winningVote)
print(winningScore)
end
end
warn(winningVote)
warn(winningScore)
game.ReplicatedStorage.Events.Choose.Vote:FireAllClients(voteCount)
return winningVote
end
local function getMobs()
votes = {}
for i, map in game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs:GetChildren() do
votes[map.Name] = {}
end
local mobName = getVotes() -- Receives nil
warn(mobName)
if mobName then
choose(mobName)
else
local mobs = game.ReplicatedStorage.Values.ChoosedMobs.RandomMobs:GetChildren()
local rndMob = mobs[math.random(1, #mobs)]
mobName = rndMob.Name
choose(mobName)
end
return mobName
end
local function getModif()
votes = {}
for i, map in game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:GetChildren() do
votes[map.Name] = {}
end
getVotes()
local modifName = getVotes()
if modifName and game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:FindFirstChild(modifName) then
chooseModif(modifName)
else
local mobs = game.ReplicatedStorage.Values.ChoosedModifier.RandomModifier:GetChildren()
local rndMob = mobs[math.random(1, #mobs)]
chooseModif(rndMob.Name)
end
return
end
game.ReplicatedStorage.Events.Choose.AddMob.OnServerEvent:Connect(getMobs)
game.ReplicatedStorage.Events.Choose.AddModif.OnServerEvent:Connect(getModif)
Oh its probably because its returning an instance. Try returning winningVote.Name and tell me if it returns a string otherwise I think id need to see more code
local VoteStarted = false
function voteMOB()
for i, v in pairs(script.Parent.Modifiers:GetDescendants()) do
if v:IsA('ImageButton') then
v.Activated:Connect(function()
script.Parent.Parent.Sounds["button.wav"]:Play()
game.ReplicatedStorage.Events.Choose.Vote:FireServer(v.Parent.Name)
end)
end
end
end
function RandomMobs()
script.Parent.Modifiers.time.Text = 'LOADING'
for i,v in pairs(script.Parent.Modifiers.choices:GetChildren()) do
if v:IsA('Frame') then
if v.Name ~= 'TEMPLATE' then
v:Destroy()
end
end
end
print('ya')
script.Parent.Modifiers.title.Text = 'Vote for a Anomaly'
game.ReplicatedStorage.Events.Choose.Time.Value = 10
script.Parent.Modifiers.Visible = true
for i = 1,3 do
local items = game.ReplicatedStorage.Values.ChoosedMobs.RandomMobs:GetChildren()
local randomItem = items[math.random(1, #items)]
local frame = script.Parent.Modifiers.choices.TEMPLATE:Clone()
frame.Parent = script.Parent.Modifiers.choices
frame.Name = randomItem.Name
frame.Visible = true
local new = randomItem:Clone()
new.Parent = frame
local boolvalue = Instance.new('BoolValue')
local imageValue = Instance.new('StringValue')
imageValue.Name = 'Image'
if i == 1 then
game.ReplicatedStorage.Events.Choose.AddVote:FireServer(randomItem)
frame.ImageButton.Image = 'rbxassetid://'..randomItem.Image.Value
frame.ImageButton.BackgroundColor3 = new:WaitForChild('Color').Value
boolvalue.Parent = frame
boolvalue.Name = new.Name
imageValue.Parent = boolvalue
imageValue.Value = randomItem.Image.Value
frame.TextLabel.Text = randomItem.Name
elseif i ==2 then
frame.ImageButton.Image = 'rbxassetid://'.. randomItem.Image.Value
game.ReplicatedStorage.Events.Choose.AddVote:FireServer(randomItem)
frame.ImageButton.BackgroundColor3 = new:WaitForChild('Color').Value
boolvalue.Parent = frame
boolvalue.Name = new.Name
imageValue.Parent = boolvalue
imageValue.Value = randomItem.Image.Value
frame.TextLabel.Text = randomItem.Name
elseif i ==3 then
frame.ImageButton.Image = 'rbxassetid://'.. randomItem.Image.Value
game.ReplicatedStorage.Events.Choose.AddVote:FireServer(randomItem)
frame.ImageButton.BackgroundColor3 = new:WaitForChild('Color').Value
boolvalue.Parent = frame
boolvalue.Name = new.Name
imageValue.Parent = boolvalue
imageValue.Value = randomItem.Image.Value
frame.TextLabel.Text = randomItem.Name
end
randomItem:Destroy()
end
game.ReplicatedStorage.Events.Choose.VoteStart:FireServer('Mob')
voteMOB()
wait(2.25)
VoteStarted = true
end
function RandomModifier()
script.Parent.Modifiers.time.Text = 'LOADING'
for i,v in pairs(script.Parent.Modifiers.choices:GetChildren()) do
if v:IsA('Frame') then
if v.Name ~= 'TEMPLATE' then
v:Destroy()
end
end
end
print('ya')
script.Parent.Modifiers.title.Text = 'Vote for a Modifier'
game.ReplicatedStorage.Events.Choose.Time.Value = 10
script.Parent.Modifiers.Visible = true
for i = 1,3 do
local items = game.ReplicatedStorage.Values.ChoosedModifier.RandomModifier:GetChildren()
local randomItem = items[math.random(1, #items)]
print(randomItem.Name)
local frame = script.Parent.Modifiers.choices.TEMPLATE:Clone()
frame.Parent = script.Parent.Modifiers.choices
frame.Name = randomItem.Name
frame.Visible = true
local new = randomItem:Clone()
new.Parent = frame
local boolvalue = Instance.new('BoolValue')
local imageValue = Instance.new('StringValue')
imageValue.Name = 'Image'
if i == 1 then
game.ReplicatedStorage.Events.Choose.AddVoteModifer:FireServer(randomItem)
frame.ImageButton.Image = 'rbxassetid://'..randomItem.Image.Value
frame.ImageButton.BackgroundColor3 = new:WaitForChild('Color').Value
boolvalue.Parent = frame
boolvalue.Name = new.Name
imageValue.Parent = boolvalue
imageValue.Value = randomItem.Image.Value
frame.TextLabel.Text = randomItem.Name
elseif i ==2 then
frame.ImageButton.Image = 'rbxassetid://'.. randomItem.Image.Value
game.ReplicatedStorage.Events.Choose.AddVoteModifer:FireServer(randomItem)
frame.ImageButton.BackgroundColor3 = new:WaitForChild('Color').Value
boolvalue.Parent = frame
boolvalue.Name = new.Name
imageValue.Parent = boolvalue
imageValue.Value = randomItem.Image.Value
frame.TextLabel.Text = randomItem.Name
elseif i ==3 then
frame.ImageButton.Image = 'rbxassetid://'.. randomItem.Image.Value
game.ReplicatedStorage.Events.Choose.AddVoteModifer:FireServer(randomItem)
frame.ImageButton.BackgroundColor3 = new:WaitForChild('Color').Value
boolvalue.Parent = frame
boolvalue.Name = new.Name
imageValue.Parent = boolvalue
imageValue.Value = randomItem.Image.Value
frame.TextLabel.Text = randomItem.Name
end
randomItem:Destroy()
end
game.ReplicatedStorage.Events.Choose.VoteStart:FireServer('Modif')
voteMOB()
wait(1.25)
VoteStarted = true
end
game.ReplicatedStorage.Events.Choose.RandomMob.OnClientEvent:Connect(RandomMobs)
game.ReplicatedStorage.Events.Choose.RandomModifier.OnClientEvent:Connect(RandomModifier)
game.ReplicatedStorage.Events.Choose.STARTVOTE.OnClientEvent:Connect(function()
script.Parent.Modifiers.fraameontop.BackgroundTransparency = 0
script.Parent.Modifiers["Bossa Me (a)"].Volume = 0
script.Parent.Modifiers["Bossa Me (a)"]:Play()
game.TweenService:Create(script.Parent.Modifiers.fraameontop,TweenInfo.new(3), {BackgroundTransparency = 1}):Play()
game.TweenService:Create(script.Parent.Modifiers["Bossa Me (a)"],TweenInfo.new(3), {Volume = 1}):Play()
end)
local choice = 0
game.ReplicatedStorage.Events.Choose.Time.Changed:Connect(function()
if game.ReplicatedStorage.Events.Choose.Time.Value == 0 then
choice += 1
-- warn(choice)
if choice == 1 or choice == 2 then
game.ReplicatedStorage.Events.Choose.AddMob:FireServer()
VoteStarted = false
script.Parent.Parent.Sounds["snap.wav"]:Play()
script.Parent.Modifiers.time.Text = 'LOADING'
RandomMobs()
end
if choice == 3 then
game.ReplicatedStorage.Events.Choose.AddMob:FireServer()
VoteStarted = false
script.Parent.Parent.Sounds["snap.wav"]:Play()
script.Parent.Modifiers.time.Text = 'LOADING'
RandomModifier()
end
if choice == 4 then
warn('end')
script.Parent.Modifiers.Visible= false
VoteStarted = false
script.Parent.Modifiers["Bossa Me (a)"]:Stop()
game.ReplicatedStorage.Events.Choose.AddModif:FireServer()
game.ReplicatedStorage.Events.Choose.STARTGAME:FireServer()
script.Parent.Parent.Sounds["swoosh.wav"]:Play()
script.Parent.Parent.Sounds["snap.wav"]:Play()
choice = 1
end
end
end)
game.ReplicatedStorage.Events.Choose.Vote.OnClientEvent:Connect(function(vote)
for name,count in vote do
local btn = script.Parent.Modifiers.choices:FindFirstChild(name)
if btn then
btn.vote.Text = count
end
end
end)
while task.wait(1) do
if VoteStarted == true then
game.ReplicatedStorage.Events.Choose.Time.Value -= 1
script.Parent.Modifiers.time.Text = game.ReplicatedStorage.Events.Choose.Time.Value
script.Parent.Parent.Sounds["Kerplunk.wav"]:Play()
end
end
(Script Voting)
local votes = {}
function ProcessVote(plr,Choice)
for i, Choice in votes do
Choice[plr.UserId] = nil
end
votes[Choice][plr.UserId] = true
print(votes)
getVotes()
end
game.ReplicatedStorage.Events.Choose.AddVote.OnServerEvent:Connect(function(plr,mob)
local value = Instance.new('BoolValue')
value.Parent = game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs
value.Name = mob.Name
local image = Instance.new('StringValue')
image.Parent = value
image.Value = mob.Image.Value
image.Name = 'Image'
local color = Instance.new('Color3Value')
color.Parent = value
color.Value = mob.Color.Value
color.Name = 'Color'
end)
game.ReplicatedStorage.Events.Choose.AddVoteModifer.OnServerEvent:Connect(function(plr,mob)
local value = Instance.new('BoolValue')
value.Parent = game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier
value.Name = mob.Name
local image = Instance.new('StringValue')
image.Parent = value
image.Value = mob.Image.Value
image.Name = 'Image'
local color = Instance.new('Color3Value')
color.Parent = value
color.Value = mob.Color.Value
color.Name = 'Color'
end)
game.ReplicatedStorage.Events.Choose.VoteStart.OnServerEvent:Connect(function(plr , set)
votes = {}
if set == 'Mob' then
for i, map in game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs:GetChildren() do
votes[map.Name] = {}
end
else
for i, map in game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:GetChildren() do
votes[map.Name] = {}
end
end
end)
game.ReplicatedStorage.Events.Choose.Vote.OnServerEvent:Connect(ProcessVote)
function getVotes()
local voteCount = {}
local winningScore = 0
local winningVote
for choice, data in votes do
-- print(votes)
voteCount[choice] = 0
for i, v in data do
voteCount[choice] += 1
end
if voteCount[choice] > winningScore then
winningVote = choice
winningScore = voteCount[choice]
-- Has Normal Info
print(winningVote)
print(winningScore)
end
end
warn(winningVote)
warn(winningScore)
game.ReplicatedStorage.Events.Choose.Vote:FireAllClients(voteCount)
return winningVote
end
local function getMobs()
votes = {}
for i, map in game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs:GetChildren() do
votes[map.Name] = {}
end
local mobName = getVotes() -- Receives nil
warn(mobName)
if mobName then
choose(mobName)
else
local mobs = game.ReplicatedStorage.Values.ChoosedMobs.RandomMobs:GetChildren()
local rndMob = mobs[math.random(1, #mobs)]
mobName = rndMob.Name
choose(mobName)
end
return mobName
end
local function getModif()
votes = {}
for i, map in game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:GetChildren() do
votes[map.Name] = {}
end
getVotes()
local modifName = getVotes()
if modifName and game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:FindFirstChild(modifName) then
chooseModif(modifName)
else
local mobs = game.ReplicatedStorage.Values.ChoosedModifier.RandomModifier:GetChildren()
local rndMob = mobs[math.random(1, #mobs)]
chooseModif(rndMob.Name)
end
return
end
game.ReplicatedStorage.Events.Choose.AddMob.OnServerEvent:Connect(getMobs)
game.ReplicatedStorage.Events.Choose.AddModif.OnServerEvent:Connect(getModif)
You emptied the votes table at the beginning of the function, so your getVotes() function had nothing to loop through. I’ve added comments to edit’s I’ve made.
local votes = {}
function ProcessVote(plr,Choice)
for i, Choice in votes do
Choice[plr.UserId] = nil
end
votes[Choice][plr.UserId] = true
print(votes)
getVotes()
end
game.ReplicatedStorage.Events.Choose.AddVote.OnServerEvent:Connect(function(plr,mob)
local value = Instance.new('BoolValue')
value.Parent = game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs
value.Name = mob.Name
local image = Instance.new('StringValue')
image.Parent = value
image.Value = mob.Image.Value
image.Name = 'Image'
local color = Instance.new('Color3Value')
color.Parent = value
color.Value = mob.Color.Value
color.Name = 'Color'
end)
game.ReplicatedStorage.Events.Choose.AddVoteModifer.OnServerEvent:Connect(function(plr,mob)
local value = Instance.new('BoolValue')
value.Parent = game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier
value.Name = mob.Name
local image = Instance.new('StringValue')
image.Parent = value
image.Value = mob.Image.Value
image.Name = 'Image'
local color = Instance.new('Color3Value')
color.Parent = value
color.Value = mob.Color.Value
color.Name = 'Color'
end)
game.ReplicatedStorage.Events.Choose.VoteStart.OnServerEvent:Connect(function(plr , set)
votes = {}
if set == 'Mob' then
for i, map in game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs:GetChildren() do
votes[map.Name] = {}
end
else
for i, map in game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:GetChildren() do
votes[map.Name] = {}
end
end
end)
game.ReplicatedStorage.Events.Choose.Vote.OnServerEvent:Connect(ProcessVote)
function getVotes()
local voteCount = {}
local winningScore = 0
local winningVote
for choice, data in votes do
voteCount[choice] = #data
-- @ EDIT: Removed unneded for loop. Reduces time to return value.
if voteCount[choice] > winningScore then
winningVote = choice
winningScore = voteCount[choice]
end
end
game.ReplicatedStorage.Events.Choose.Vote:FireAllClients(voteCount)
return winningVote
end
local function getMobs()
-- votes = {}
-- @ EDIT: You emptied the votes table. So when you tried to loop through it, it was empty. Moved to bottom of function
for i, map in game.ReplicatedStorage.Values.ChoosedMobs.SelectMobs:GetChildren() do
votes[map.Name] = {}
end
local mobName = getVotes() -- Receives nil
warn(mobName)
if mobName then
choose(mobName)
else
local mobs = game.ReplicatedStorage.Values.ChoosedMobs.RandomMobs:GetChildren()
local rndMob = mobs[math.random(1, #mobs)]
mobName = rndMob.Name
choose(mobName)
end
votes = {}
return mobName
end
local function getModif()
-- votes = {}
-- @ EDIT: You emptied the votes table. So when you tried to loop through it, it was empty. Moved to bottom of function
for i, map in game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:GetChildren() do
votes[map.Name] = {}
end
getVotes()
local modifName = getVotes()
if modifName and game.ReplicatedStorage.Values.ChoosedModifier.SelectModifier:FindFirstChild(modifName) then
chooseModif(modifName)
else
local mobs = game.ReplicatedStorage.Values.ChoosedModifier.RandomModifier:GetChildren()
local rndMob = mobs[math.random(1, #mobs)]
chooseModif(rndMob.Name)
end
votes = {}
return
end
game.ReplicatedStorage.Events.Choose.AddMob.OnServerEvent:Connect(getMobs)
game.ReplicatedStorage.Events.Choose.AddModif.OnServerEvent:Connect(getModif)