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

How can i save the player's team with a datastore?

Asked by 4 years ago
Edited 4 years ago

I am making a game right now and i wanna know how to save the player's team like the game "The Impossible Obby", basically i want it to saves the player's team when the player leaves and when the player reachs another spawn location (with a different team).

3 answers

Log in to vote
0
Answered by
8jxms 9
4 years ago

Just use the DataStoreService and make the team a value. Here is a whole tutorial on Save Data Keep in mind SaveData is an advanced scripting service.

0
But what kind of value do i need for a team? MythsList 38 — 4y
0
For example, assign each team a number value such as a red team being 1 green being 2 etc. Save that value to the player and load it when they join. 8jxms 9 — 4y
0
So i add number values to each teams in the Teams category? MythsList 38 — 4y
0
You could also use DataStore2, which is much easier. killerbrenden 1537 — 4y
View all comments (2 more)
0
i don't think it is easier :| MythsList 38 — 4y
0
However you do it, it will probably require a DataStore. You can always use someone else if it's to advanced for you. 8jxms 9 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Well, I'm pretty sure you can retreive a team from a name (string) and a name (string) from a team. So from the team the player is in take the name, and store it in a datastore, then when they join again later take the name of the team from the datastore and assign the player to the team with that name.

0
local team = player.Team(TeamName)? MythsList 38 — 4y
0
To get the team's name: player.Team.Name, to get the team object: game.Teams:FindFirstChild(team_name) BeAHacker666 75 — 4y
0
local team = game.Teams:FindFirstChild(player.Team.Name) ? MythsList 38 — 4y
0
if you just want the Team object player.Team is enough. BeAHacker666 75 — 4y
Log in to vote
0
Answered by 4 years ago

This is how I would do it.

ModuleScript in ServerScriptService and I named it "TeamModule" -

local teamModule = {}

local DataStore2 = require(1936396537)

DataStore2.Combine("MainKey","Team") -- You Can Change MainKey To Any New Value To Reset The DataBase

local defaultTeam = "None"

function teamModule:SetPlayerTeam(player)
    if player:IsA("Player") then
        local excessData = Instance.new("Folder",player)
        excessData.Name = "ExcessData"

        local teamValue = Instance.new("StringValue",excessData)
        teamValue.Name = "Team"

        local teamStore = DataStore2("Team",player)

        local function updateTeam(team)
            player.ExcessData.Team.Value = team
        end

        updateTeam(teamStore:Get(defaultTeam))

        teamStore:OnUpdate(updateTeam)
    else
        error("The First Parameter Has To Be A Player")
    end
end

return teamModule

This is a ServerScript in ServerScriptService and I named it TeamSet -

local teamModule = require(script.Parent.TeamModule)

local DataStore2 = require(1936396537)

local RE = game:GetService("ReplicatedStorage"):WaitForChild("Events")

game.Players.PlayerAdded:Connect(function(player)
    teamModule:SetPlayerTeam(player)
    wait(1)
    local team = game:GetService("Teams"):FindFirstChild(player.ExcessData.Team.Value).TeamColor
    wait()
    player.TeamColor = team
    wait(0.25)
    player:LoadCharacter()
end)

RE.TeamChange.OnServerEvent:Connect(function(player,team)
    player.TeamColor = game:GetService("Teams"):FindFirstChild(team).TeamColor
    player.ExcessData.Team.Value = team
    local teamStore = DataStore2("Team",player)
    teamStore:Set(team)
    wait(0.1)
    player:LoadCharacter()
end)

And Then I have a folder in ReplicatedStorage called Events, and you should keep all RemoteEvents and Functions in there, and it will save their team. All you have to do is have the first team enabled Auto-Assignable, and the other teams should not have that enabled. It will get their team, and then respawn them on their team at that spawn. It should work, if it doesn't, let me know so I can tweak it and fix it for you.

0
And for it to work in studio if you want to test it, you have to put a BoolValue inside of ServerStorage and name it SaveInStudio and have it enabled to true for it to work in studio. killerbrenden 1537 — 4y
0
when i leave and rejoin i'm still in the DefaultTeam and i have no error MythsList 38 — 4y
0
what do i have to edit? MythsList 38 — 4y
0
but i get an error when i set the DefaultTeam a team with letters in it and not numbers. MythsList 38 — 4y
View all comments (5 more)
0
So, you should have 2 teams in Teams and one should be auto-assignable, and one isn't. How it works is, it takes your current team, and cache's it to the server, and when you leave it saves. Currently, when you are on the starting team, it saves you and starts you on the starting team, if you're on a different team, you will be saved there when you leave, but when you come back you will be on... killerbrenden 1537 — 4y
0
on the beginning team for a couple of seconds, and then you will be switched over to the other team after the game loads in. So you will be saved at that team. If you want, you can add me on roblox and I can show you in my game that it works. killerbrenden 1537 — 4y
0
I set a value inside the player as a string variable as their team, so the team name has to be a string without numbers, and the default team has to be a string without numbers using " " for it to work. killerbrenden 1537 — 4y
0
This is what i did, but it gave me an error saying "Team" = nil value MythsList 38 — 4y
0

Answer this question