I'm making a script where your random magic element saves/loads when you come back into the game but it's always a new element! Please help me with this script, thanks.
local myElementData = game:GetService("DataStoreService"):GetDataStore("ElementData")
--Element Table
Elements = {"Fire","Water","Wind","Lightning", "Earth"}
--DataStore
function onPlayerAdded(player)
repeat wait() until player.Character
local stats = Instance.new("StringValue")
stats.Parent = player
stats.Name = "stats"
local myElement = Instance.new("StringValue")
myElement.Parent = stats
myElement.Name = "myElement"
if myElementData:GetAsync("Element_"..player.Name) ~= nil then
myElement.Value = myElementData:GetAsync(player.Name)
print(myElement.Value)
else --If it didn't load
local RandomElement = math.random(1, #Elements)
myElement.Value = Elements[RandomElement]
print(myElement.Value)
myElement.Value = myElementData:UpdateAsync(player.name)
end
myElement.Changed:connect(function(Val)
myElementData:SetASync("Element "..player.Name, Val)
end)
end
game.Players.PlayerAdded:connect(onPlayerAdded)

3 comments
Ok so I recently found a few syntax errors like player.name(no caps) and (idk if this is a syntax error but) i seperated the "Element" .. player.Name with space(before it was attached to the words). MY NEW ERROR IS NOW ARGUMENT 2 IS MISSING OR NIL.
line 32 should be myElementData:SetAsync() instead of myElementData:SetASync(). Also, which line did you get the "Argument 2 missing or nil" on?
I noticed, on line 32, you save as "Element "..player.Name, but on line 16 you load "Element_"..player.Name