What's the issue with my data store?

Im making a boxing game and need saving data the script doesn’t work and doesn’t print anything, any help?

game.Players.PlayerAdded:Connect(function(player)
	local DataStoreService = game:GetService("DataStoreService")
	local goldStore = DataStoreService:GetDataStore("PLRstrength")
	local playerUserID = player.UserId
	local PLRstrength = player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value
	print("hI")
	game.Players.PlayerRemoving:Connect(function(player)
		local playerUserId = "Player_"..player.UserId

		PLRstrength:SetAsync(playerUserId, {

			player.Character.StatsLocal.LocalPlayerStrength.Value
		})
		print("Hi")
	end)

	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserID)
	end)
	if getSuccess then
		print("hi")
		player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
	end
end)
3 Likes

Use profile service: Save your player data with ProfileService! (DataStore Module) - Resources / Community Resources - DevForum | Roblox

Btw, don’t use the YouTube tutorials, they don’t tell you how to do it right. Just copy the example code the guy gives, and then go on from there figuring it out. Also, just save their data when they leave when using this.

1 Like

Don’t think throwing something at someone and saying to figure it out will help me, the code looks like spaghetti, I dont want to waste 5 hours trying to figure it out.

2 Likes

Well, if dealing with the data loss of SetAsync when your game reaches high traffic and having over 300 players’ weight on your shoulders, then that’s your choice.

1 Like

I would rather use a method that has been used and memorised by most developers then a new method that might have bugs.

1 Like

Uh this method is used in like all front-page games made in around 2019 or more and even Bedwars…

listen, im sorry im not a super advanced scripter and I dont want to deal with something without a tutorial

1 Like

You don’t need to be super advanced at all, just copy his example code, paste it in a test place, and go on from there. Trust me, I have a game that hit a peak of 2k players and an average of 800, and I wish I implemented profile service a lot more ealier.

Put the player removing function outside of the player joining function, like this:

game.Players.PlayerAdded:Connect(function(player)
	local DataStoreService = game:GetService("DataStoreService")
	local goldStore = DataStoreService:GetDataStore("PLRstrength")
	local playerUserID = player.UserId
	local PLRstrength = player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value
	print("hI")

	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserID)
	end)
	if getSuccess then
		print("hi")
		player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	PLRstrength:SetAsync(playerUserId, {

		player.Character.StatsLocal.LocalPlayerStrength.Value
	})
	print("Hi")
end)

Might not fix the issue but it doesn’t make sense to nest it inside it.

Is this a server script in ServerScriptService?

I have a question, is there a way to wait for the players character?

Yeah, like this:

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        -- This code only runs when the player's character is added.
    end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    -- Do player removing stuff here.
end)

This?

player:WaitForChild("Character")

Note: player is Player value

Screen Shot 2022-08-10 at 11.17.35 PM
what do i do about this?

it all works exept:

		local getSuccess, currentGold = pcall(function()
			return goldStore:GetAsync(playerUserID)
		end)
		if getSuccess then
			print("hi")
			player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
		end

it doesent set the value to the number
all 3 his printed

Try the code like this:

local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PLRstrength")

game.Players.PlayerAdded:Connect(function(player)
	print("hI")

	local playerUserId = "Player_"..player.UserId
	
	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(playerUserId)
	end)

	if getSuccess then
		print("hi")
		player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = currentGold
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local PLRstrength = player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value
	
	goldStore:SetAsync(playerUserId, PLRstrength)
	
	print("Hi")
end)

Also, make sure the “studio access to API services” setting is enabled if it isn’t already.

First go to game settings:

Then go to Security and the setting is there:

it says
Screen Shot 2022-08-10 at 11.30.26 PM
when i leave

I just don’t get how it will be possible to get the value

you need to keep the value in the player not the character it is destroyed when they player leaves the game instantly or keep it in an attribute on the player – the best practice is keeping a table in your save script of each players data then update as it changes that way no matter what you have a table to save then you set it to nil after the save

how would I do that? Im confused

StatsLocal where do you create that folder and the strength values?