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)
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)
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)
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.
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