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

How do you save a string value in datastore?

local DataStoreService = game:GetService("DataStoreService")
local Magic = DataStoreService:GetDataStore("Magic") -- These will create 2 new data stores called HasMagic and HasMagic as they do not exist yet
local HasMagic = DataStoreService:GetDataStore("HasMagic") -- 

game.Players.PlayerAdded:connect(function(plr)
	local key = "user_"..plr.userId -- UserId's are unique and stay the same for each player so that's why I am using this (it will also account for name changes)
	
	local Magic = Instance.new("StringValue",plr)
	Magic.Name = "Magic"
	Magic.Value = Magic:GetAsync(key) or "" -- Having problems here...
	local HasMagic = Instance.new("BoolValue",Magic)
	HasMagic.Name = "HasMagic"
	HasMagic.Value = HasMagic:GetAsync(key) or 1 -- Loads the value of the HasMagic or makes it 1 if they haven't played before.

	
	HasMagic.Changed:connect(function() HasMagic:SetAsync(key, HasMagic.Value) end) -- These save the HasMagic and HasMagic when their Magic update
	Magic.Changed:connect(function() Magic:SetAsync(key, Magic.Value) end) 
	

end)

I would like your help thanks! -- Ciyu123

1 comment

0

Any error messages?

1 answer

I see a major issue. You defined Magic and HasMagic twice, one for the datastore, one inside the function. Having local overwrites any previously defined variables with the new set one, to be used inside the function. All in all, rename the variables on line 2 and 3, then change the corresponding variables throughout the script. Hope I helped!

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