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

How do I save what Team a player is on?

Asked by 4 years ago

So what I attempted to do is have a character earn currency which puts them on a different team. For example, if a player earned 100 cash they would be on a team called Club 100 and so on. I already have the currency saved but what I learned was that when a player leaves a sever and joins again, they spawn back at Club 0. How can I use datastore to save what Team the player is on?

1 answer

Log in to vote
0
Answered by
St_vnC 330 Moderation Voter
4 years ago

Use DataStore silly :

local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService(“DataStore”) -- Name this whatever you want

local function loadStarterData(Player) -- Basically this is the function that creates the stats
      local leaderstats = Instance.new(“Folder”)
      leaderstats.Name = “leaderstats”
      leaderstats.Parent = Player

      local Cash = Instance.new(“IntValue”)
      Cash.Name = “Cash” -- Name your currency
      Cash.Parent = leaderstats
end

local function loadData(Player) -- Function that loads the data
      local Data

      local s,e pcall(function()
             Data = DataStore:GetAsync(“UserId:”.. Player.UserId)
      end)
      if s then
            Player.leaderstats.Cash.Value = Data[1]
            Player.Team = game.Teams[Data[1]]
            print(“Loaded ”.. Player.Name ..”’s data successfully”)
      else
            print(“Failed to load ”.. Player.Name ..”’s data”)
      end

end

local function saveData(Player) -- Function that saves the data
      local Data = {
         Cash = 0,
         Team = “”
       }

      Data.Cash = Player.leaderstats.Cash.Value
      Team = Player.Team.Name

      local s,e pcall(function()
             DataStore:SetAsync(“UserId:”.. Player.UserId, Data)
      end)
      if s then
            print(“Saved ”.. Player.Name ..”’s data successfully”)
      else
            print(“Failed to save ”.. Player.Name ..”’s data”)
      end

end

game.Players.PlayerAdded:Connect(function(Player)
       loadStarterData(Player)
       loadData(Player)    
end)

game.Players.PlayerRemoving:Connect(function(Player)
       saveData(Player)
end)
0
I feel like this is pretty complex, make sure to check everytime when the cash.value has changed to change the team St_vnC 330 — 4y
0
Nice example. But you didn't explain it. If you don't "really" help them, then they will keep coming for more code. And they will not learn something only copy and paste. :/ [It's just my opinion] Foxy_Developer 111 — 4y
0
Ok basically SetAsync() is what saves the data and GetAsync() is what loads the data you can go check more in the roblox wiki since I’m too lazy to explain in general St_vnC 330 — 4y
0
On line 2 you forgot to put :GetDataStore() Trading_Opportunity 191 — 4y
View all comments (4 more)
0
^ He’s right tho St_vnC 330 — 4y
0
So I implemented your method of saving the teams into my existing code but I got a weird error that I can't fix. It seems like a simple one but I'm bad at debugging so I'm just going to ask for assistance. The code is chitterness 4 — 4y
0
What is the error? St_vnC 330 — 4y
0
The error is " 'Team' is only used in the enclosing function; consider changing it to local". It's on line 25 of the code I sent as an answer cause it's the only way I know how too show it to you chitterness 4 — 4y
Ad

Answer this question