Firstly, I have a Module script that controls the DataStore functions (GetAsync, SetAsync, etc.), which is here;
local dataManipulation = {} local dss=game:GetService("DataStoreService"):GetDataStore("HasPlayed","PlayerData") function dataManipulation:GetPlayerData(player,key) dss:GetAsync(key) print("Got "..player.Name.."'s Data") end function dataManipulation:SavePlayerData(player,key,value) local success,message=pcall(function() dss:UpdateAsync(key,function(oldvalue) local newVal=oldvalue or 0 newVal=value return newVal end) end) if not success then print("AN ERROR OCCURRED: "..'"'..message..'"') else print("Saved "..player.Name.."'s Data!") end end function dataManipulation:SaveForLeavingPlayer(player,key,value) dss:SetAsync(key,value) print("Saved "..player.Name.."'s Data (player left)") end return dataManipulation
What I'm trying to do is save a Value when the new player joins and says, "I understand", this is the Server Script;
game.Players.PlayerAdded:connect(function(player) player.PlayerGui:WaitForChild("Intro") player.Chatted:connect(function(msg) if string.sub(string.lower(msg),1,12)==string.lower("I Understand") then pu:FireClient(player) end end) end)
This is the Local Script;
local player=game.Players.LocalPlayer local pu=game.ReplicatedStorage:WaitForChild("PlayerUnderstands") local ds=require(game:GetService("ServerScriptService"):WaitForChild("DataModule")) local intro=script.Intro:Clone() intro.Parent=player.PlayerGui pu.OnClientEvent:connect(function() intro:Destroy() local hasPlayed=player.PlayerStats.HasPlayed hasPlayed.Value=true local valueToSave={hasPlayed.Value} ds:SavePlayerData(player,player.UserId,valueToSave) end)
And I have the Load and Save script here;
local ds=require(game.ServerScriptService.DataModule) game.Players.PlayerAdded:connect(function(player) local data=ds:GetPlayerData(player,player.UserId) if data then print("Data Found") else print("No data found") end end) game.Players.PlayerRemoving:connect(function(player) local valuesToSave={player.PlayerStats.HasPlayed.Value} ds:SaveForLeavingPlayer(player,player.UserId,valuesToSave) end)
When I join and say "I understand" it says it saves, but when I join again it says "No data found".