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

Why won't this datastore load/save?

Asked by 9 years ago

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)


0
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. zipper01 62 — 9y
0
line 32 should be myElementData:SetAsync() instead of myElementData:SetASync(). Also, which line did you get the "Argument 2 missing or nil" on? ChipioIndustries 454 — 9y
0
I noticed, on line 32, you save as "Element "..player.Name, but on line 16 you load "Element_"..player.Name Thewsomeguy 448 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

Line 26, myElement.Value = myElementData:UpdateAsync(player.Name) does not supply the existing value. To fix this, you must use GetAsync(player.Name).

Upvote if this is helpful!

Ad

Answer this question