Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My DataStore script won't save/load?

Asked by
markjac -11
5 years ago

My DataStore script won't save or maybe won't load players data. I have a button in game that raises the values of GoldRubyC and CollectionValue


local datastore = game:GetService("DataStoreService"):GetDataStore("tablohazerjia1") game.Players.PlayerAdded:connect(function(player) local PlayerProfile = Instance.new("Folder") PlayerProfile.Name = "PlayerProfile" PlayerProfile.Parent = player -------------------- [Collected Treasure Verified]- local TreasuresCollected = Instance.new("IntValue") TreasuresCollected.Name = "TreasuresCollected" TreasuresCollected.Parent = PlayerProfile --------------------------------------------------- ---------------------[Treasures]-------------------- local GoldRubyC = Instance.new("IntValue") GoldRubyC.Name = "GoldRubyC" GoldRubyC.Parent = PlayerProfile.TreasuresCollected --------------------------------------------------- ---------------------[Collection Value]------------ local CollectionValue = Instance.new("IntValue") CollectionValue.Name = "CollectionValue" CollectionValue.Parent = PlayerProfile -------------------------------------------------- local key = "user_" .. player.userId local storeditems = datastore:GetAsync(key) if storeditems then TreasuresCollected.Value = storeditems[1] GoldRubyC.Value = storeditems[2] CollectionValue.Value = storeditems[3] else local items = {TreasuresCollected.Value, GoldRubyC.Value, CollectionValue.Value} datastore:SetAsync(key, items) end end) game.Players.PlayerRemoving:connect(function(player) local items = {player.PlayerProfile.TreasuresCollected.Value, player.PlayerProfile.TreasuresCollected.GoldRubyC.Value, player.PlayerProfile.CollectionValue.Value} local key = "user_" ..player.userId datastore:SetAsync(key, items) end)
0
Go into your game through your profile, go into the command page and give yourself these values, tell me if they save when you do it that way Hizar7 102 — 5y

1 answer

Log in to vote
0
Answered by
Hizar7 102
5 years ago

Hi mark, the problem is you're trying to get the values through a local script. you're going to need to use a server script. Use a remote Event from the local script to tell the server script to add the points, ill demonstrate here. Put a RemoteEvent inside the ReplicatedStorage, its an addable item through right click options. then put this into the local script after the button has been pressed.

game.ReplicatedStorage.RemoteEvent:FireServer()

then add this to a server script

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)

then add underneath that line to add the values to the folder. like this

local myName = "TreasuresCollected"
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)

local TreasuresCollected = plr.PlayerProfile:FindFirstChild(myName)
      TreasuresCollected.Value =TreasuresCollected.Value + 1
end)

let me know if this works!

0
if this works please accept this answer, I'd appreciate it ! Hizar7 102 — 5y
Ad

Answer this question