Not saving/loading values datastore

Script:

local dss = game:GetService("DataStoreService")
local ds = dss:GetGlobalDataStore()

game.Players.PlayerAdded:Connect(function(plr)
	
	local plrUserId = plr.UserId
	
	local medals = Instance.new("Folder",plr)
	medals.Name = "Medals"
	
	local bronze = Instance.new("BoolValue",medals)
	bronze.Name = "BronzeStar"
	
	
	local silver = Instance.new("BoolValue",medals)
	silver.Name = "SilverStar"
	
	
	local heart = Instance.new("BoolValue",medals)
	heart.Name = "PurpleHeart"
	heart.Value = false
	
	local merit = Instance.new("BoolValue",medals)
	merit.Name = "LegionOfMerit"
	merit.Value = false
	
	local ac = Instance.new("BoolValue",medals)
	ac.Name = "AfghanistanCampaign"
	ac.Value = false
	
	local plrData = ds:GetAsync(plrUserId)
	
	local getSuccess,errorMessage = pcall(function()

		bronze.Value = plrData[1] or false
		silver.Value = plrData[2] or false
		heart.Value = plrData[3] or false
		merit.Value = plrData[4] or false
		ac.Value = plrData[5] or false
		
		print("loaded")
		
	end)

	if not getSuccess then

		warn(errorMessage)

	end
	
	while wait(30) do
		
		local setSuccess,errorMessage = pcall(function()

			ds:SetAsync(plrUserId, {
				
				bronze.Value,
				silver.Value,
				heart.Value,
				merit.Value,
				ac.Value
				
			})

		end)

		if not setSuccess then

			warn(errorMessage)

		end
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local plrUserId = plr.UserId
	
	local setSuccess,errorMessage = pcall(function()

		ds:SetAsync(plrUserId,{
			
			plr.Medals.BronzeStar.Value,
			plr.Medals.SilverStar.Value,
			plr.Medals.PurpleHeart.Value,
			plr.Medals.LegionOfMerit.Value,
			plr.Medals.AfghanistanCampaign.Value
			
		})

	end)

	if not setSuccess then

		warn(errorMessage)

	end
	
end)

I’m trying to use a global data store so I can access and change it from different locations (such as my other missions giving a new medal to the player) whenever they get a new player the boolean value is set to true coordinating with the medals name.

For some reason it won’t work, and all it says in the console is an error from the if not getSuccess then warning line and it says:

ServerScriptService.Med als:35: attempt to index boolean with number - Server - Medals:47

What am I doing wrong?

2 Likes

I got something to do rq so im just gonna point on the likely cause

bronze.Value = plrData[1] or false
	silver.Value = plrData[2] or false
	heart.Value = plrData[3] or false
	merit.Value = plrData[4] or false
	ac.Value = plrData[5] or false

i have no idea what this means

also you should read up on this guide Roblox Lua Style guide

1 Like

just try reading up on the article I linked

im not exactly sure what this has to do with much, but ill try fixing a few things from what i saw in it

try checking what’s saved in the datastore at that key in case it’s not empty
maybe at one point, you accidentally did something like :SetAsync(plrUserId, true) and now, whenever you do plrData[1], it’s erroring because you’re pretty much doing true[1]

You had a pcall in the wrong place and some other stuff. See if this works:

local dss = game:GetService("DataStoreService")
local ds = dss:GetGlobalDataStore()

game.Players.PlayerAdded:Connect(function(plr)
	local plrUserId = plr.UserId

	local medals = Instance.new("Folder",plr)
	medals.Name = "Medals"

	local bronze = Instance.new("BoolValue",medals)
	bronze.Name = "BronzeStar"

	local silver = Instance.new("BoolValue",medals)
	silver.Name = "SilverStar"

	local heart = Instance.new("BoolValue",medals)
	heart.Name = "PurpleHeart"

	local merit = Instance.new("BoolValue",medals)
	merit.Name = "LegionOfMerit"

	local ac = Instance.new("BoolValue",medals)
	ac.Name = "AfghanistanCampaign"

	local getSuccess, errorMessage = pcall(function()
		plrData = ds:GetAsync(plrUserId)
	end)

	if getSuccess then
		if plrData ~= nil then
			bronze.Value = plrData.BronzeStar
			silver.Value = plrData.SilverStar
			heart.Value = plrData.PurpleHeart
			merit.Value = plrData.LegionOfMerit
			ac.Value = plrData.AfghanistanCampaign
		else
			bronze.Value = false
			silver.Value = false
			heart.Value = false
			merit.Value = false
			ac.Value = false
		end
	else
		warn(errorMessage)
	end

	spawn(function()
		while true do
			local setSuccess,errorMessage = pcall(function()
				ds:SetAsync(plrUserId, {
					BronzeStar = bronze.Value,
					SilverStar = silver.Value,
					PurpleHeart = heart.Value,
					LegionOfMerit = merit.Value,
					AfghanistanCampaign = ac.Value
				})
			end)

			if not setSuccess then
				warn(errorMessage)
			end
			wait(30)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local plrUserId = plr.UserId

	local DataToSave = {
		BronzeStar = plr.Medals.BronzeStar.Value,
		SilverStar = plr.Medals.SilverStar.Value,
		PurpleHeart = plr.Medals.PurpleHeart.Value,
		LegionOfMerit = plr.Medals.LegionOfMerit.Value,
		AfghanistanCampaign = plr.Medals.AfghanistanCampaign.Value
	}

	local setSuccess,errorMessage = pcall(function()
		ds:SetAsync(plrUserId, DataToSave)
	end)

	if not setSuccess then
		warn(errorMessage)
	end
end)