Help improving my vote script

I made this vote code and tested it with some people. There are 4 bars on the baseplate that show how much votes what color has, like a graph and it also updates across all servers. The thing is the vote didn’t count for one guy and I have no idea what the problem is. I am not experienced enough with roblox data stores and may made some mistakes. Please help me out to fix this


local dss = game:GetService("DataStoreService")
local votes = dss:GetDataStore('Votes')
local didvote = dss:GetDataStore('DidVote')
local tweenService = game:GetService('TweenService')
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)
local meters = {workspace.Red.Meter,workspace.Yellow.Meter,workspace.Green.Meter,workspace.Blue.Meter}
local texts = {workspace.Red.Brick.SurfaceGui.TextLabel,workspace.Yellow.Brick.SurfaceGui.TextLabel,workspace.Green.Brick.SurfaceGui.TextLabel,workspace.Blue.Brick.SurfaceGui.TextLabel}
local numtotext = {'Red','Yellow','Green','Blue'}
local buttons = {workspace.Red.Button,workspace.Yellow.Button,workspace.Green.Button,workspace.Blue.Button}
local buttonInfoIn = TweenInfo.new(.2,Enum.EasingStyle.Sine,Enum.EasingDirection.In)
local buttonInfoOut = TweenInfo.new(.2,Enum.EasingStyle.Sine,Enum.EasingDirection.Out)
local metermax = 18
local rs = game:GetService('ReplicatedStorage')
local sendmsg = rs.sendmsg

for i = 1, 4 do
	local debounce = false
	buttons[i].ClickDetector.MouseClick:Connect(function(player)
		if debounce == true then
			return
		end
		local success, did = pcall(function()
			return didvote:GetAsync('Player_'..player.UserId)
		end)
		if success then
			if did == true then
				sendmsg:FireClient(player,'You have already voted!')
				return
			end
			debounce = true
			didvote:SetAsync('Player_'..player.UserId,true)
			votes:IncrementAsync(numtotext[i],1)
			buttons[i].Sound:Play()
			tweenService:Create(buttons[i],buttonInfoIn,{Size=Vector3.new(1,.2,1)}):Play()
			wait(.2)
			tweenService:Create(buttons[i],buttonInfoOut,{Size=Vector3.new(1,1,1)}):Play()
			wait(.3)
			debounce = false
		else
			sendmsg:FireClient(player,'Unable to access data')
		end
	end)
end

function update()
	local success,vote_table = pcall(function()
		local r,y,g,b = votes:GetAsync('Red'),votes:GetAsync('Yellow'),votes:GetAsync('Green'),votes:GetAsync('Blue')
		return {r,y,g,b}
	end)
	if success then
		for i = 1, 4 do
			vote_table[i] = vote_table[i] or 0
		end
		local max = math.max(vote_table[1],vote_table[2],vote_table[3],vote_table[4])
		for i = 1, 4 do
			texts[i].Text = vote_table[i]
			vote_table[i] /= max
			tweenService:Create(meters[i],tweenInfo,{Size=Vector3.new(2,metermax*vote_table[i],0)}):Play()
		end
	end
end
update()
while wait(2) do
	update()
end
3 Likes

i think this is too much


does this work if not then move to #help-and-feedback:scripting-support end

maybe do
game.ReplicatedStorage

1 Like

Never do game.<ServiceNameHere>, it’s bad practice, and OP made the right decision to use game:GetService("<ServiceNameHere>")

EDIT: For services you can see in the explorer, it’s fine to use game.<ServiceNameHere>, most developers use game:GetService("<ServiceNameHere>") for all services just to be safe and or to keep the habit of using it for other services.

2 Likes

Using this instead of datastore may be better for your purposes.

Also instead of using seperarte GetAsync calls like for your rygb, I would just use 1 get async call and have it return a table, less chance of failure and also there limited getasync calls you can make per second so better to avoid throttling

update()
while wait(2) do
	update()
end

You can rewrite this as

while true do
    update()
    task.wait(2)
end

Generally, you should avoid doing while yield do as it is less efficient and usually not practical

local texts = {workspace.Red.Brick.SurfaceGui.TextLabel,workspace.Yellow.Brick.SurfaceGui.TextLabel,workspace.Green.Brick.SurfaceGui.TextLabel,workspace.Blue.Brick.SurfaceGui.TextLabel}

This could be a lot shorter if you just group it and then use a getchildren for loop to insert it for you

1 Like