Someone help with my code and Datastores

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to optimize my code and fix it too
  2. What is the issue? Include screenshots / videos if possible!
    The code dosent work
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I already posted a topic about datastores because i didn’t knew if i had to use global datastores ordered datastores but no one answered
    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!
local Replicated_Storage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local TextService = game:GetService("TextService")

local DataStoreService = game:GetService("DataStoreService")

local Dares = DataStoreService:GetOrderedDataStore("DaresV1")
local Questions = DataStoreService:GetOrderedDataStore("QuestionsV1")

local Modules = Replicated_Storage:WaitForChild("Modules")
local Remotes = Replicated_Storage:WaitForChild("Remotes")
local Announce = Remotes:WaitForChild("Announce")
local TruthOrDare = Remotes:WaitForChild("TruthOrDare")
local Vote = Remotes:WaitForChild("Vote")
local ServerMessage = Remotes:WaitForChild("ServerMessage")
local Bottle = workspace:WaitForChild("Boba")
	local Bottle_Sounds = Bottle:WaitForChild("Sounds")

local Sessions = require(script.Parent.Sessions)
local Seats_Session = Sessions.Seat_Sessions


local Playing = true

local Chairs_Rotation_Thing = {
	["Chair1"] = 75,
	["Chair2"] = 30,
	["Chair3"] = -15,
	["Chair4"] = -60,
	["Chair5"] = -105,
	["Chair6"] = -150,
	["Chair7"] = 165,
	["Chair8"] = 120
}

local Seats = workspace.Forest_Map.Shack.Main.Chairs
local NameTags = Replicated_Storage:WaitForChild("NameTags")

for ind,chair in pairs(Seats:GetChildren()) do
	chair.Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
		if chair.Seat.Occupant ~= nil then
			local Char = chair.Seat.Occupant.Parent
			local plr = game.Players:FindFirstChild(Char.Name)

			if Char and plr and Char:FindFirstChild('Humanoid') then
				table.insert(Seats_Session, plr)
				Char.Humanoid.JumpPower = 0
			end			
		end
	end)
end


function spin(chair)
	local tweenService: TweenService = game:GetService("TweenService")

	local tween: Tween = tweenService:Create(Bottle, TweenInfo.new(1, Enum.EasingStyle.Circular), {
		Orientation = Bottle.Orientation + Vector3.new(0, 0, 1080)
	})
	
	local tween3: Tween = tweenService:Create(Bottle, TweenInfo.new(.5, Enum.EasingStyle.Circular), {
		Orientation = Bottle.Orientation + Vector3.new(0, 0, 75)
	})
	
	local tween2 = tweenService:Create(Bottle, TweenInfo.new(0.5, Enum.EasingStyle.Circular), {
		Orientation = Vector3.new(-89,-168, Chairs_Rotation_Thing[chair])
	})

	tween:Play()
	
	
	task.wait(0.65)
	--tween3:Play()
	--task.wait(0.6)
	--print('tween3 completed playing tween 2')
	tween2:Play()
	task.wait(1)
	
	task.wait(0.6)
end

local choosen_player = nil
local response = nil
local voting = false
local Votes = {}

TruthOrDare.OnServerEvent:Connect(function(player, msg)
	if response == nil and player == choosen_player then
		if msg == "Truth" then
			response = "Truth"
		elseif msg == "Dare" then
			response = "Dare"
		end
	end
end)

function checkvote(player)
	for i, vote in pairs(Votes) do
		if i == player.Name then
			return true
		end
	end
	return false
end

Vote.OnServerEvent:Connect(function(player, val)
	if player ~= choosen_player and voting and checkvote(player) == false then -- check vote = false mean dosent find player in Votes
		if val == true then
			Votes[player.Name] = true
		elseif val == false then
			Votes[player.Name] = false
		end
	end
end)


function StartVotes()
	if game.PrivateServerId == "" and game.PrivateServerOwnerId == 0 and #game.Players:GetPlayers() >= 3 then
		table.clear(Votes)
		voting = true
		Vote:FireAllClients()
		Bottle_Sounds.Clock_Ticking.Playing = true

		for i = -15, 0 do
			Announce:FireAllClients("Votes end in " .. math.abs(i) .. " seconds.")
			wait(1)
		end
		Bottle_Sounds.Clock_Ticking.Playing = false
		wait(0.25)

		local trueVotes = 0
		local falseVotes = 0

		for playerName, vote in pairs(Votes) do
			if vote == true then
				trueVotes = trueVotes + 1
			else
				falseVotes = falseVotes + 1
			end
		end

		local totalVotes = trueVotes + falseVotes

		if falseVotes > (totalVotes / 2) then
			local falsePercentage = (falseVotes / totalVotes) * 100
			Announce:FireAllClients("Majority voted false (" .. string.format("%.2f", falsePercentage) .. "%)")
		else
			local truePercentage = (trueVotes / totalVotes) * 100
			Announce:FireAllClients("Majority voted true (" .. string.format("%.2f", truePercentage) .. "%)")
		end
		Bottle_Sounds.Response:Play()
		wait(5)
	elseif game.PrivateServerId ~= "" and game.PrivateServerOwnerId ~= 0 then
		Announce:FireAllClients("Votes Are Disabled In Private Servers.")
	elseif #game.Players:GetPlayers() < 3 then
		Announce:FireAllClients("Not Enough Players To Vote.")
	end
end



function checkifplayer(player)
	if table.find(Seats_Session, player) then
		return true
	end
	return false
end

local questionning = false
local responded = false

local last_player = nil
local choosen_tag = nil

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(chat)
		if plr == choosen_player and questionning then
			responded = chat
		end
	end)
end)

function intialize()
	if choosen_tag then
		choosen_tag:Destroy()
	end
	response = nil
	choosen_player = nil
	responded = false
	questionning = false
	voting = false
	table.clear(Votes)
end

while true do
	if #Seats_Session > 1 then
		Bottle_Sounds.Clock_Ticking.Playing = true
		for i=-5,0 do 
			Announce:FireAllClients("Next Player Getting Choosen in "..math.abs(i).." seconds")
			wait(1)
			if Playing == false then
				break
			end
		end
		
		Bottle_Sounds.Clock_Ticking.Playing = false
		local function ChoosePlayer()
			local playing = #Seats_Session
			local rdm_player = math.random(1, playing)

			local choosen_player = Seats_Session[rdm_player]

			return choosen_player
		end
		
		repeat
			choosen_player = ChoosePlayer()
		until last_player ~= choosen_player

		last_player = choosen_player
		
		for ind, chair in pairs(Seats:GetChildren()) do
			if chair.Seat.Occupant ~= nil then
				
				local Char = chair.Seat.Occupant.Parent
				local plr = game.Players:FindFirstChild(Char.Name)

				if Char and plr and plr.Name == choosen_player.Name then
					spin(chair.Name)
					
					Bottle_Sounds.Response:Play()
					
					choosen_tag = NameTags.Choosen:Clone()
					choosen_tag.Parent = Char.Head

					Announce:FireAllClients("Truth or Dare ? "..choosen_player.Name..".")	
					TruthOrDare:FireClient(choosen_player)

					Bottle_Sounds.Clock_Ticking.Playing = true
					
					local Attempts = 0
					
					repeat
						wait(1)
						Attempts += 1
						Announce:FireAllClients("Truth or Dare ? "..choosen_player.Name..". (".. 15 - Attempts ..")")
						if Attempts == 15 then
							Announce:FireAllClients(choosen_player.Name.." Took too much time to respond.")
							break
						elseif choosen_player == nil then
							Announce:FireAllClients("Player left the game. He will be punished.")
							break
						elseif checkifplayer(choosen_player) == false then
							Announce:FireAllClients("Choosen player has left the table.")
							break
						elseif #Seats_Session < 2 then
							Announce:FireAllClients("Not Enough Players To Continue this round.")
							break
						end

					until response ~= nil
					
					if response == nil then
						Announce:FireAllClients("Player Took Too Much Time To Choose.")
					end
					
					Bottle_Sounds.Clock_Ticking.Playing = false
					
					if response ~= nil and Attempts < 15 and choosen_player ~= nil and table.find(Seats_Session, choosen_player) then
						Announce:FireAllClients(choosen_player.Name.." Chose "..string.upper(response)..".")
						ServerMessage:FireAllClients(choosen_player.Name.." Chose "..string.upper(response)..".")
						Bottle_Sounds.Response:Play()
						
						wait(2)

						if response == "Truth" then
							local pages = Questions:ListKeysAsync(nil, 100)
							local counter = 0
							while true do
								for _, key in pairs(pages:GetCurrentPage()) do
									counter += 1
									warn(key.KeyName)
								end
								if pages.IsFinished then
									break	-- Exit loop if we've looped over all pages
								end
								-- Get new page (same API limit as calling 'ListKeysAsync')
								pages:AdvanceToNextPageAsync()
							end

							Announce:FireAllClients("Picking a Random Question from "..counter.." Questions.")
							
							wait(3)
							
							local randomIndex = math.random(1, counter)

							local success5, errormsg5 = nil, nil 
							local attempt5 = 1 -- how many attemps

							repeat -- attempt to get data
								success5, entity = pcall(function()
									return Questions:GetAsync(randomIndex)
								end)
								attempt5 += 1
								if not success5 then
									warn(entity)
									task.wait(3)
								end
							until success5 or attempt5 == 5

							warn(entity)

							if checkifplayer(choosen_player) == true then
								questionning = true
								Announce:FireAllClients(choosen_player.Name .. "... " .. tostring(entity))
								ServerMessage:FireAllClients(choosen_player.Name .. "... " .. tostring(entity))
								for i=-70, 0 do
									Announce:FireAllClients(choosen_player.Name .. "... " .. tostring(entity) .. "(".. math.abs(i) .. ")")
									
									if checkifplayer(choosen_player) == false or responded then	questionning = false break end
									wait(1)
								end
								
								if checkifplayer(choosen_player) and responded ~= false then
									wait(1)
									Announce:FireAllClients(choosen_player.Name.." Responded : ".. responded)
									ServerMessage:FireAllClients(choosen_player.Name.." Responded : ".. responded)
									
									-- feature to make players vote if they should give him gems
									wait(8)
								end
							else
								intialize()
								break
							end


						elseif response == "Dare" then							
							local pages = Dares:ListKeysAsync(nil, 100)
							local counter = 0
							while true do
								for _, key in pairs(pages:GetCurrentPage()) do
									counter += 1
									warn(key.KeyName)
								end
								if pages.IsFinished then
									break	-- Exit loop if we've looped over all pages
								end
								-- Get new page (same API limit as calling 'ListKeysAsync')
								pages:AdvanceToNextPageAsync()
							end

							Announce:FireAllClients("Picking a Random Dare from "..counter.." Dares.")
							
							wait(3)
							
							local randomIndex = math.random(1, counter)

							local success5, errormsg5 = nil, nil 
							local attempt5 = 1 -- how many attemps
							
							repeat -- attempt to get data
								success5, entity = pcall(function()
									return Dares:GetAsync(randomIndex)
								end)
								attempt5 += 1
								if not success5 then
									warn(entity)
									task.wait(3)
								end
							until success5 or attempt5 == 5

							warn(entity)
						end
					end
					
					StartVotes()
					wait(5)
					Announce:FireAllClients("Starting a New Round...")
					
					intialize()
					break
				end
			end
		end
	else
		Announce:FireAllClients("Waiting for more players to start...")
		task.spawn(function()
			spin("Chair1")
		end)
	end
	wait(3)
end

So basically, i don’t know which datastore to use because i’m making a Truth or Dare game and try to save the dares and questions in two different globaldatastores but the problem is there is a part in the code where it print how much keys there is in the global datastores and everytime it says “Picking a random question from 13 questions” even tho there is only 3-4 questions in the questions global datastore.

Also this script is the RoundsManager script it basically pick a random player in the table and make the bottle spin.

1 Like

why would you save truth or dare questions in a datastore?

So the community, with chatfilter can use it too to add Questions in the database with robuxs.
And also admins can add questions or remove questions too from an admin panel.

1 Like

That’s a lot of code to scroll through and try to figure out what’s going on. Can you tell us the issue? Are there any errors or warnings in the output?

1 Like

that’s a broken code here is the approximatively fixed code :

 local Replicated_Storage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local TextService = game:GetService("TextService")

local DataStoreService = game:GetService("DataStoreService")

local Dares = DataStoreService:GetGlobalDataStore("DaresV1")
local Questions = DataStoreService:GetGlobalDataStore("QuestionsV1")

local Modules = Replicated_Storage:WaitForChild("Modules")
local Remotes = Replicated_Storage:WaitForChild("Remotes")
local Announce = Remotes:WaitForChild("Announce")
local TruthOrDare = Remotes:WaitForChild("TruthOrDare")
local Vote = Remotes:WaitForChild("Vote")
local ServerMessage = Remotes:WaitForChild("ServerMessage")
local Bottle = workspace:WaitForChild("Boba")
	local Bottle_Sounds = Bottle:WaitForChild("Sounds")

local Sessions = require(script.Parent.Sessions)
local Seats_Session = Sessions.Seat_Sessions


local Playing = true

local Chairs_Rotation_Thing = {
	["Chair1"] = 75,
	["Chair2"] = 30,
	["Chair3"] = -15,
	["Chair4"] = -60,
	["Chair5"] = -105,
	["Chair6"] = -150,
	["Chair7"] = 165,
	["Chair8"] = 120
}

local Seats = workspace.Forest_Map.Shack.Main.Chairs
local NameTags = Replicated_Storage:WaitForChild("NameTags")

for ind,chair in pairs(Seats:GetChildren()) do
	chair.Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
		if chair.Seat.Occupant ~= nil then
			local Char = chair.Seat.Occupant.Parent
			local plr = game.Players:FindFirstChild(Char.Name)

			if Char and plr and Char:FindFirstChild('Humanoid') then
				table.insert(Seats_Session, plr)
				Char.Humanoid.JumpPower = 0
			end			
		end
	end)
end


function spin(chair)
	local tweenService: TweenService = game:GetService("TweenService")

	local tween: Tween = tweenService:Create(Bottle, TweenInfo.new(1, Enum.EasingStyle.Circular), {
		Orientation = Bottle.Orientation + Vector3.new(0, 0, 1080)
	})
	
	local tween3: Tween = tweenService:Create(Bottle, TweenInfo.new(.5, Enum.EasingStyle.Circular), {
		Orientation = Bottle.Orientation + Vector3.new(0, 0, 75)
	})
	
	local tween2 = tweenService:Create(Bottle, TweenInfo.new(0.5, Enum.EasingStyle.Circular), {
		Orientation = Vector3.new(-89,-168, Chairs_Rotation_Thing[chair])
	})

	tween:Play()
	
	
	task.wait(0.65)
	--tween3:Play()
	--task.wait(0.6)
	--print('tween3 completed playing tween 2')
	tween2:Play()
	task.wait(1)
	
	task.wait(0.6)
end

local choosen_player = nil
local response = nil
local voting = false
local Votes = {}

TruthOrDare.OnServerEvent:Connect(function(player, msg)
	if response == nil and player == choosen_player then
		if msg == "Truth" then
			response = "Truth"
		elseif msg == "Dare" then
			response = "Dare"
		end
	end
end)

function checkvote(player)
	for i, vote in pairs(Votes) do
		if i == player.Name then
			return true
		end
	end
	return false
end

Vote.OnServerEvent:Connect(function(player, val)
	if player ~= choosen_player and voting and checkvote(player) == false then -- check vote = false mean dosent find player in Votes
		if val == true then
			Votes[player.Name] = true
		elseif val == false then
			Votes[player.Name] = false
		end
	end
end)


function StartVotes()
	if game.PrivateServerId == "" and game.PrivateServerOwnerId == 0 and #game.Players:GetPlayers() >= 3 then
		table.clear(Votes)
		voting = true
		Vote:FireAllClients()
		Bottle_Sounds.Clock_Ticking.Playing = true

		for i = -15, 0 do
			Announce:FireAllClients("Votes end in " .. math.abs(i) .. " seconds.")
			wait(1)
		end
		Bottle_Sounds.Clock_Ticking.Playing = false
		wait(0.25)

		local trueVotes = 0
		local falseVotes = 0

		for playerName, vote in pairs(Votes) do
			if vote == true then
				trueVotes = trueVotes + 1
			else
				falseVotes = falseVotes + 1
			end
		end

		local totalVotes = trueVotes + falseVotes

		if falseVotes > (totalVotes / 2) then
			local falsePercentage = (falseVotes / totalVotes) * 100
			Announce:FireAllClients("Majority voted false (" .. string.format("%.2f", falsePercentage) .. "%)")
		else
			local truePercentage = (trueVotes / totalVotes) * 100
			Announce:FireAllClients("Majority voted true (" .. string.format("%.2f", truePercentage) .. "%)")
		end
		Bottle_Sounds.Response:Play()
		wait(5)
	elseif game.PrivateServerId ~= "" and game.PrivateServerOwnerId ~= 0 then
		Announce:FireAllClients("Votes Are Disabled In Private Servers.")
	elseif #game.Players:GetPlayers() < 3 then
		Announce:FireAllClients("Not Enough Players To Vote.")
	end
end



function checkifplayer(player)
	if table.find(Seats_Session, player) then
		return true
	end
	return false
end

local questionning = false
local responded = false

local last_player = nil
local choosen_tag = nil

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(chat)
		if plr == choosen_player and questionning then
			responded = chat
		end
	end)
end)

function intialize()
	if choosen_tag then
		choosen_tag:Destroy()
	end
	response = nil
	choosen_player = nil
	responded = false
	questionning = false
	voting = false
	table.clear(Votes)
end

while true do
	if #Seats_Session > 1 then
		Bottle_Sounds.Clock_Ticking.Playing = true
		for i=-5,0 do 
			Announce:FireAllClients("Next Player Getting Choosen in "..math.abs(i).." seconds")
			wait(1)
			if Playing == false then
				break
			end
		end
		
		Bottle_Sounds.Clock_Ticking.Playing = false
		local function ChoosePlayer()
			local playing = #Seats_Session
			local rdm_player = math.random(1, playing)

			local choosen_player = Seats_Session[rdm_player]

			return choosen_player
		end
		
		repeat
			choosen_player = ChoosePlayer()
		until last_player ~= choosen_player

		last_player = choosen_player
		
		for ind, chair in pairs(Seats:GetChildren()) do
			if chair.Seat.Occupant ~= nil then
				
				local Char = chair.Seat.Occupant.Parent
				local plr = game.Players:FindFirstChild(Char.Name)

				if Char and plr and plr.Name == choosen_player.Name then
					spin(chair.Name)
					
					Bottle_Sounds.Response:Play()
					
					choosen_tag = NameTags.Choosen:Clone()
					choosen_tag.Parent = Char.Head

					Announce:FireAllClients("Truth or Dare ? "..choosen_player.Name..".")	
					TruthOrDare:FireClient(choosen_player)

					Bottle_Sounds.Clock_Ticking.Playing = true
					
					local Attempts = 0
					
					repeat
						wait(1)
						Attempts += 1
						Announce:FireAllClients("Truth or Dare ? "..choosen_player.Name..". (".. 15 - Attempts ..")")
						if Attempts == 15 then
							Announce:FireAllClients(choosen_player.Name.." Took too much time to respond.")
							break
						elseif choosen_player == nil then
							Announce:FireAllClients("Player left the game. He will be punished.")
							break
						elseif checkifplayer(choosen_player) == false then
							Announce:FireAllClients("Choosen player has left the table.")
							break
						elseif #Seats_Session < 2 then
							Announce:FireAllClients("Not Enough Players To Continue this round.")
							break
						end

					until response ~= nil
					
					if response == nil then
						Announce:FireAllClients("Player Took Too Much Time To Choose.")
					end
					
					Bottle_Sounds.Clock_Ticking.Playing = false
					
					if response ~= nil and Attempts < 15 and choosen_player ~= nil and table.find(Seats_Session, choosen_player) then
						Announce:FireAllClients(choosen_player.Name.." Chose "..string.upper(response)..".")
						ServerMessage:FireAllClients(choosen_player.Name.." Chose "..string.upper(response)..".")
						Bottle_Sounds.Response:Play()
						
						wait(2)

						if response == "Truth" then
							local pages = Questions:ListKeysAsync(nil, 100)
							local counter = 0
							while true do
								for _, key in pairs(pages:GetCurrentPage()) do
									counter += 1
									warn(key.KeyName)
								end
								if pages.IsFinished then
									break	-- Exit loop if we've looped over all pages
								end
								-- Get new page (same API limit as calling 'ListKeysAsync')
								pages:AdvanceToNextPageAsync()
							end

							Announce:FireAllClients("Picking a Random Question from "..counter.." Questions.") -- problem here because it prints picking a random question from 13 dares even tho there is only 4 question in the datastore
							
							wait(3)
							
							local randomIndex = math.random(1, counter)

							local success5, errormsg5 = nil, nil 
							local attempt5 = 1 -- how many attemps

							repeat -- attempt to get data
								success5, entity = pcall(function()
									return Questions:GetAsync(randomIndex)
								end)
								attempt5 += 1
								if not success5 then
									warn(entity)
									task.wait(3)
								end
							until success5 or attempt5 == 5

							warn(entity)

							if checkifplayer(choosen_player) == true then
								questionning = true
								Announce:FireAllClients(choosen_player.Name .. "... " .. tostring(entity))
								ServerMessage:FireAllClients(choosen_player.Name .. "... " .. tostring(entity))
								for i=-70, 0 do
									Announce:FireAllClients(choosen_player.Name .. "... " .. tostring(entity) .. "(".. math.abs(i) .. ")")
									
									if checkifplayer(choosen_player) == false or responded then	questionning = false break end
									wait(1)
								end
								
								if checkifplayer(choosen_player) and responded ~= false then
									wait(1)
									Announce:FireAllClients(choosen_player.Name.." Responded : ".. responded)
									ServerMessage:FireAllClients(choosen_player.Name.." Responded : ".. responded)
									
									-- feature to make players vote if they should give him gems
									wait(8)
								end
							else
								intialize()
								break
							end


						elseif response == "Dare" then							
							local pages = Dares:ListKeysAsync(nil, 100)
							local counter = 0
							while true do
								for _, key in pairs(pages:GetCurrentPage()) do
									counter += 1
									warn(key.KeyName)
								end
								if pages.IsFinished then
									break	-- Exit loop if we've looped over all pages
								end
								-- Get new page (same API limit as calling 'ListKeysAsync')
								pages:AdvanceToNextPageAsync()
							end

							Announce:FireAllClients("Picking a Random Dare from "..counter.." Dares.") -- problem here because it prints picking a random dare from 13 dares even tho there is only 4 dares

							
							wait(3)
							
							local randomIndex = math.random(1, counter)

							local success5, errormsg5 = nil, nil 
							local attempt5 = 1 -- how many attemps
							
							repeat -- attempt to get data
								success5, entity = pcall(function()
									return Dares:GetAsync(randomIndex)
								end)
								attempt5 += 1
								if not success5 then
									warn(entity)
									task.wait(3)
								end
							until success5 or attempt5 == 5

							warn(entity)
						end
					end
					
					StartVotes()
					wait(5)
					Announce:FireAllClients("Starting a New Round...")
					
					intialize()
					break
				end
			end
		end
	else
		Announce:FireAllClients("Waiting for more players to start...")
		task.spawn(function()
			spin("Chair1")
		end)
	end
	wait(3)
end

i commented the part where there is a problem.

It’s commented with –

it’s in the “if response == “Truth” then etc etc you will see it”