Hi everyone!
So I was implementing data store into my game to save the number of survivals and coins a player has after they exit (I'm new to data store). I used a couple examples I found to make a data store like this and I made a module script any script can access. So I use the module script in 2 other scripts: 1 in the server script service, which sets up the leader board and the data store, and another script which changes the number of coins and survivals (both on the leaderboard and the data store) when its parent (a part) is touched by a player. Here are the scripts:
ModuleScript named "Datastore"
local DataStoreModule = {} local DataStore = game:GetService("DataStoreService") local playerdata = DataStore:GetDataStore("playerdata") function DataStoreModule.onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Survivals = Instance.new("IntValue") Survivals.Name = "Survivals" Survivals.Parent = leaderstats local Coins = Instance.new("IntValue") Coins.Name = "Coins" Coins.Parent = leaderstats local data local data2 local success, errormessage = pcall(function() data = playerdata:GetAsync(player.UserId .. "-Survivals") data2 = playerdata:GetAsync(player.UserId .. "-Coins") end) if success then Survivals.Value = data Coins.Value = data2 print("Player ID " .. player.UserId ..": Data fully created!") else warn("Player ID " .. player.UserId .. ": There was an error while making data (" .. errormessage .. ")") end end function DataStoreModule.SaveData(player) local success, errormessage = pcall(function() playerdata:SetAsync(player.UserId .. "-Survivals", player.leaderstats.Survivals.Value) playerdata:SetAsync(player.UserId .. "-Coins", player.leaderstats.Coins.Value) end) if success then print("Player ID " .. player.UserId .. ": Data fully saved!") else warn("Player ID " .. player.UserId .. ": There was an error while saving data (" .. errormessage .. ")") end end return DataStoreModule
Leaderboard script
DataStore = require(game.Workspace.Datastore) game.Players.PlayerAdded:Connect(DataStore.onPlayerJoin) game.Players.PlayerRemoving:Connect(DataStore.SaveData)
Script for when the part is hit by the player ("HitScript")
local Players = game:GetService("Players") local DataStore = require(game.Workspace.Datastore) Main = require(game.Workspace.Main) Survivors = Main.Survivors InList = false script.Parent.Touched:Connect(function(h) if h.Parent:FindFirstChild("Humanoid") then h.Parent.HumanoidRootPart.CFrame = game.Workspace.Lobby.SpawnLocation.CFrame + Vector3.new(0,7,0) for i = 1, #Survivors do local Survivor = Survivors[i] if Survivor == h.Parent.Name then InList = true break end end if InList == false then table.insert(Survivors, #Survivors + 1, h.Parent.Name) local player = Players:GetPlayerFromCharacter(h.Parent) if not player then return end game.ReplicatedStorage.WinPadHit:FireClient(player) DataStore.SaveData(player) end end end)
For some reason, the data isnt saved when the hit script runs (I touch the part), but when I mannually change the survivals and coins values using the dev console, data store works fine. The prints also work when the hit script runs, but no change happens.
Any help is appreciated!