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

Can someone help with storing a string inside of a DataStore?

I'm currently attempting to store a StringValue inside of the DataStore so when the player leaves, the datastore will still be the same. My code is below, as well as the error I'm receiving.


local DataStoreService = game:GetService("DataStoreService")

local data= DataStoreService:GetDataStore("data") 



game.Players.PlayerAdded:connect(function(plr)
	local key = "user_"..plr.userId
	
	local database = Instance.new("IntValue",plr)
	database.Name = "leaderstats"
	
	local data= Instance.new("StringValue",database)
	data.Name = "Level"
	data.Value = Level:GetAsync(key)
	
	
function onTagsChanged(plr, tags)


	
	
end
	data.Changed:connect(function() data(key, data.Value) end)
end)

The error:

21:05:02.808 - ServerScriptService.Script:16: bad argument #3 to 'Value' (string expected, got nil) 21:05:02.809 - Stack Begin 21:05:02.809 - Script 'ServerScriptService.Script', Line 16 21:05:02.809 - Stack End

1 comment

0

Hope I helped.

1 answer

When a player joins your game for the first time, he/she has no data saved, so there is no data to load! Therefore on line 15, you're setting the value to a nil value, since they have no previously saved data. I recommend setting it to

data.Value = Level:GetAsync(key) or "None" -- or whatever you want the default value to be.

Also, you never defined Level? Or are you just not showing that..

5 comments

0

Not showing that tidbit.

0

Alright, well the answer is still there.

0

Now I'm getting this in the console: 22:30:13.595 - GetAsync is not a valid member of StringValue 22:30:13.596 - Stack Begin 22:30:13.596 - Script 'ServerScriptService.Script', Line 16 22:30:13.596 - Stack End

0

And the Level tidbit was from another script I was working on. If you'd like chat more directly, message me on ROBLOX if you'd like. I'll explain what I'm trying to do there.

0

Um.. What edit did you make? That error shouldn't have come form my code.

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