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

How do you save a string value in datastore?

Asked by 9 years ago
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

0
Any error messages? fahmisack123 385 — 9y

1 answer

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

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!

Ad

Answer this question