2 issues (Soultion Found :DDDDDD)

Issue 1: Save Data script won’t save data.

Issue 2: My “sell script” isn’t working. The game is a simulator and it wont take the player’s souls, and turn it into coins.

Save Data Script:

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")


local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local souls = Instance.new("IntValue") --Sets up value for leaderstats

	souls.Name = "Souls"

	souls.Parent = leaderstats



	local coins = Instance.new("IntValue") --Sets up value for leaderstats

	coins.Name = "Coins"

	coins.Parent = leaderstats



	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		souls.Value = data['Souls']

		coins.Value = data['Coins']

	else

		-- Data store is working, but no current data for this player

		souls.Value = 0

		coins.Value = 0

	end

end


local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end


local function onPlayerExit(player)  --Runs when players exit



	local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)


	if not success then

		warn('Could not save data!')

	end

end


game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

Sell Script:

local part=script.Parent
part.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local leaderstats = player:WaitForChild("SaveData")
			local coins = leaderstats.Coins
			local selling = leaderstats.Souls
			if selling.Value>=0 then
				coins.Value=coins.Value+selling.Value*1
				selling.Value=0
				
			end
		end
	end
end)
1 Like

In these lua blocks you find whatever I could change with my limited scripting knowledge :/.

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")


game.Players.PlayerAdded:Connect(function(player) 
-- when a player connects it runs a function, idrk I'm doing this wth a reference

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player
game.Players.PlayerRemoving:Connect(function(player) 
-- no idea but you know.... reference :)



	local player_stats = create_table(player)

	local success, errormessage = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)
2 Likes

you are not saving when you shutdown the game. When you click the stop button when testing you are shutting down the server, not leaving the game.

local part=script.Parent
part.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local leaderstats = player:WaitForChild("SaveData")
			local coins = leaderstats.Coins
			local selling = leaderstats.Souls
			if selling.Value>=0 then
				coins.Value=coins.Value+selling.Value*1
				selling.Value=0
				
			end
		end
	end
end)

coins.Value = coins.Value - selling.Value*1

I think…

1 Like

Resolving the first issue, you need to either play it normally for datastoring to work, or enable Studio Access to API services in game settings > security for studio in order for datastores to work while playtesting

For the second problem, you’re waiting for the object “SaveData” on line 10, you should actually be looking for “leaderstats” which is where you’re actually storing and referencing the data.

Sadly the save data doesn’t work when I play the game normally…
And selling still doesn’t work even though i changed it to leaderstats

Interesting, because I tested the code and both worked fine for me after those fixes, did you change any of it since you posted?

1 Like

Ya… I enabled API, and published and checked it on roblox. I put leaderstats instead of save data. I’m so confused :sob:

Edit: The save data saves my data once, though never again… Though on normal roblox it doesnt at all.

didn’t work. Thanks for trying to help though :slight_smile:

Just to make sure, your sell script is inside the part you want, correct?

1 Like

Ya it is…


Oh, additionally, make sure you are changing the data on the server and not the client

how do i tell?


If you’re testing in studio, you can switch between “Client” and “Server” if you go to the top and go to the test tab.

the button looks like this:
testimg

Learn this first: Client-Server Model | Roblox Creator Documentation

I cant find the button. Where is it?
Edit: Nvm found it

I switch between server and client. Though I don’t understand how that would resolve the issue

I understand the basics or server and client. How they work and what not.

Your data and shop scripts are server scripts, if you attempt to change data via the client, it doesn’t replicate if you try to change it via the explorer in studio when playing from the client, and as such, won’t save the data you changed from the client nor will it change your “Souls” into “Coins”.

Additionally, you could change it via the dev console with a line of code.

Edit: the same applies for the studio-based cmdbar where you have to be set to the server to change information on the server

1 Like

Ok ye that makes sense, now then what should I change in the code so that it runs using the server not client.