Hello,
I am making a test quest system and figured string values would help out. I am trying to figure out how to save the string values value with data stores. I also am trying to figure out how to make it so if say a event happened, it would change the string values value and save it. What I have so far isn’t working, and I am unsure how to approach this as there isn’t much material existing on this topic.
Data Store Script:
local dataStore = game:GetService("DataStoreService"):GetDataStore("Quests")
game.Players.PlayerAdded:Connect(function(plr)
local questext = Instance.new("StringValue", plr)
questext.Name = "Quest"
local playerUserId = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(tostring(playerUserId))
end)
if success then
if data ~= nil then
questext.Value = "Funny"
print(questext.Value)
else
questext.Value = "No funny"
print(questext.Value)
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserId = "Player_"..plr.UserId
local data = {plr.Character.Quest.Value}
local success, errormessage = pcall(function()
dataStore:SetAsync(tostring(playerUserId), data)
end)
if success then
print("Data successfully saved.")
else
print("Error when saving data.")
warn(errormessage)
end
end)
Part Script Changing Value:
local click = script.Parent
click.MouseClick:Connect(function(Player)
Player.Character:FindFirstChild('Quest').Value = "Funny"
end)
I have a error on line 33 of the data store script saying lua ServerScriptService.Script:33: attempt to index nil with 'Quest'
Okay but I still don’t understand how to fix this. I also don’t understand how i’d make it save the value of string value when it gets changed via the mouse click.
local dataStore = game:GetService("DataStoreService"):GetDataStore("Quests")
game.Players.PlayerAdded:Connect(function(plr)
local questext = Instance.new("StringValue", plr)
questext.Name = "Quest"
local playerUserId = "Player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(tostring(playerUserId))
end)
if success then
if data ~= nil then
questext.Value = "Funny"
print(questext.Value)
else
questext.Value = "No funny"
print(questext.Value)
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserId = "Player_"..plr.UserId
local data = {plr.Quest.Value}
local success, errormessage = pcall(function()
dataStore:SetAsync(tostring(playerUserId), data)
end)
if success then
print("Data successfully saved.")
else
print("Error when saving data.")
warn(errormessage)
end
end)
local playerUserId = "Player_"..plr.UserId
local data = {plr.Quest.Value}
local success, errormessage = pcall(function()
dataStore:SetAsync(tostring(playerUserId), data)
end)
if success then
print("Data successfully saved.")
else
print("Error when saving data.")
warn(errormessage)
end
paste this in the function BUT make sure that this is in the same script as the datastore
No need to do that, only save when you exit the game and/or save every 30 minutes in case of something happening. No need to save everytime you change value.
I tried this but for some reason its not saving the value the mouse click is giving. I was testing and changing the value to “Not funny 2” on the mouse click but when you go back in the game it just goes back to funny.
Alright so. I have a click detector that is supposed to change the string values value. This works, but the data store script is not saving this value. It keeps printing as if the player has no existing data.