DataStore works fine when only one person is ingame, but changes everyone's values with multiple?
This datastore always works with the money, by the way. My issue is that the values in ReplicatedStorage change whenever someone else joins the server, and that replicates to all the clients.
LocalScript inside of Dances folder:
01 | local loadAnimEvent = game:GetService( "ReplicatedStorage" ):WaitForChild( "Remotes" ):WaitForChild( "loadAnimEvent" ) |
03 | loadAnimEvent.OnClientEvent:connect( function (plr) |
04 | print ( "dances loadin'" ) |
05 | local plr = game.Players.LocalPlayer |
06 | for i,e in pairs (game.ReplicatedStorage.danceValues:GetChildren()) do |
07 | if e:IsA( "BoolValue" ) and e.Value = = true then |
09 | for i,v in pairs (plr.PlayerGui.Dances:GetChildren()) do |
10 | if v:IsA( "TextButton" ) and v.Name = = e.Name then |
11 | v:Clone().Parent = plr.PlayerGui.dancesGui.dancesFrame.danceFrame.danceScrolling |
DataStore script:
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local ds = DataStoreService:GetDataStore( "danceSave" ) |
03 | local ds 2 = DataStoreService:GetDataStore( "MoneySave" ) |
05 | game:GetService( "Players" ).PlayerAdded:Connect( function (Player) |
07 | local Stats = Instance.new( 'Folder' ) |
08 | Stats.Name = "leaderstats" |
10 | local Money = Instance.new( 'NumberValue' , Stats) |
20 | local CanSave = Instance.new( 'BoolValue' , Player) |
22 | CanSave.Name = "CanSaveData" |
25 | local DataFetchSuccess, ErrorMessage = pcall ( function () |
26 | moneyData = ds 2 :GetAsync( tostring (Player.UserId)) |
27 | dancesData = ds:GetAsync( tostring (Player.UserId)) |
29 | if DataFetchSuccess then |
30 | if moneyData ~ = nil then |
31 | print ( "Money save found" ) |
32 | Money.Value = moneyData |
34 | print ( "No money save found." ) |
39 | if dancesData ~ = nil then |
42 | for i,v in pairs (dancesData) do |
43 | local Dance = game.ReplicatedStorage.danceValues:FindFirstChild(v) |
46 | game.ReplicatedStorage:WaitForChild( "Remotes" ).loadAnimEvent:FireClient(Player) |
51 | Player.CanSaveData.Value = false |
52 | Player:Kick( "Your data failed to load! Please rejoin." ) |
56 | game:GetService( "Players" ).PlayerRemoving:Connect( function (Player) |
57 | if Player.CanSaveData.Value = = false then return end |
59 | local moneyData = Player.leaderstats.Money.Value |
60 | local dancesToSave = { } |
65 | for i,v in pairs (game:GetService( "ReplicatedStorage" ).danceValues:GetChildren()) do |
66 | if v:IsA( "BoolValue" ) and v.Value = = true then |
67 | table.insert(dancesToSave, v.Name) |
72 | local DataWriteSuccess, ErrorMessage = pcall ( function () |
73 | print ( "Saving " ..Player.Name.. "s stats" ) |
74 | ds:SetAsync( tostring (Player.UserId), dancesToSave) |
76 | ds 2 :SetAsync( tostring (Player.UserId), moneyData) |
80 | if not DataWriteSuccess then |
83 | while Retry_Count < 6 do |
85 | local Succeed, Error = pcall ( function () |
86 | print ( "Attempting to save " ..Player.Name.. "'s stats." ) |
87 | ds:SetAsync( tostring (Player.UserId), dancesToSave) |
88 | ds 2 :SetAsync( tostring (Player.UserId), moneyData) |
90 | if Succeed then print ( "Saved " ..Player.Name.. "'s stats" ) break end |
91 | Retry_Count = Retry_Count + 1 |
Could this be fixed if I replicated the Folder with the dances in ReplicatedStorage to the player whenever the player joins? And then refer to the player instead of ReplicatedStorage?
Thanks.