Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

everyone's data saves the same value among other issues?

--set core values and load data-----
local plr = game.Players:FindFirstChild(script.Parent.Name)
repeat wait() until plr


local datastore = game:GetService("DataStoreService")
	local mydata = datastore:GetDataStore("mydata")
local value = script.Parent.Gems
local data
	local success, fail = pcall(function()
		data = mydata:GetAsync(plr.UserId.."Gems")
	end)
	if success then
		value.Value = data
	else
		print("data load failed")
		warn(fail)
	end
----------------------------------


--save data--
local debounce = false

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, fail = pcall(function()
		mydata:SetAsync(plr.UserId.."Gems",value.Value)
	end)
	if success then
		print("data saved")
		script.Parent:Destroy()
	else
		print("an error has occured while saving")
		warn(fail)
	end
end)


local changedebounce = false
local char = plr.Character
char.Humanoid.Died:Connect(function()
	if not changedebounce then
		changedebounce = true
		
		local success, fail = pcall(function()
		mydata:SetAsync(plr.UserId.."Gems",value.Value)
	end)
	if success then
		print("data saved")
		script.Parent:Destroy()
	else
		print("an error has occured while saving")
		warn(fail)
	end
	end
	wait(2)
	
end)
-----------------

	
	
	

this is my datastore for saving gems but I ran into issues. one of them is that everyone keeps saving the same value, so if one person has 100 gems then another person who loads their data now has 100 gems. im not sure what is going on as it should be saving data with a player's playerID.

some help with this and any other issues would be GREATLY appreciated.

1 answer

✓ Accepted answer

That's because you aren't using playerAdded event.

this

local data
    local success, fail = pcall(function()
        data = mydata:GetAsync(plr.UserId.."Gems")
    end)
    if success then
        value.Value = data
    else
        print("data load failed")
        warn(fail)
    end

Should look like this

players.PlayerAdded:Connect(function(plr)
    local data
    local success, fail = pcall(function()
        data = mydata:GetAsync(plr.UserId.."Gems")
    end)
    if success then
        value.Value = data
    else
        print("data load failed")
        warn(fail)
    end
end)

Also it looks like your data script is one for each player and that's horrible. You should make it one and do it so that it handles all players

This discussion is preserved as it was. New answers, comments, and votes are closed.