Using Data Store: Team Save.Changing Teams:27: attempt to index global 'DataStoreService' (a nil value)

I’m new to scripting, but I have this code to save a player’s team. I’m not sure what is wrong with it.

local players = game:GetService(“Players”)
local noTeam = game:GetService(“Teams”)[“No Team”]
local team = game:GetService(“Teams”)[“Labyrinth Hunters”]
local team2 = game:GetService(“Teams”)[“Maze Runners”]

– Add a function to run when the brick is touched.
workspace.LHblock.Touched:Connect(function(hit)
– Check that the thing touching the brick is a player and that they have no team.
local player = players:GetPlayerFromCharacter(hit.Parent)
if (player and player.TeamColor == noTeam.TeamColor) then
– Change the player’s team.
player.Team = team
player.TeamColor = team.TeamColor
end
end)

workspace.MRblock.Touched:Connect(function(hit)
local player = players:GetPlayerFromCharacter(hit.Parent)
if(player and player.TeamColor == noTeam.TeamColor) then
player.Team = team2
player.TeamColor = team2.TeamColor
end
end)

local DataSotreService = game:GetService(“DataStoreService”)

local myDataSote = DataStoreService:GetDataStore(“myDataStore”)

myDataStore:SetAsync(player.UserId…“-player”,player.team.teamColor)

Thanks for your time. I also couldn’t figure out how to put my code in a block so it’s easier to read, how do I do that?

2 Likes

picture
highlight your code and click that

you just spelt “DataStoreService” wrong when retrieving it

1 Like

You have misspelled DataSotreService. The variable name should be DataStoreService instead.

First please indent your code to make it easier to read, second your data store structure is way off and makes no sense. Where do you use GetAsync()? Please look at this article for more detail:

Here is a video AlvinBlox made if you are still struggling:

The name of your variable is spelled wrong. You declared it as “DataSotreService” and later tried to reference it as “DataStoreService”

More importantly the lines that save data are not in the right scope. The script is not going to know what “player” is because it is not inside the event listeners.

Also this method you’re using is going to hit your data store limits rather quickly since you will be making calls to the datastore every time a part is touched. Just save the player’s team color when they exit the game.