Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't this datastore load/save?

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

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.

0

line 32 should be myElementData:SetAsync() instead of myElementData:SetASync(). Also, which line did you get the "Argument 2 missing or nil" on?

0

I noticed, on line 32, you save as "Element "..player.Name, but on line 16 you load "Element_"..player.Name

1 answer

✓ Accepted answer

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.