This would have been my attempt if I was in your place. I would first have created a bool value inside the player. Let's call it 'HasItem'. Whenever you will have to change this value maybe from inside a local script, you will just change this bool value. Now our main challenge will be to save and load this bool value.
This is going to be my script inside serverscriptservice:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Value = 0
Clicks.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 2000000000
Coins.Parent = leaderstats
end)
This is your code so far.
Now I will add the bool value just like Clicks and Coins. But I won't add it to the leaderstats since we don't want to show the bool value on the leaderboard. Thus, I am going to make a sepearte folder for it. Like this:
local HiddenFolder = Instance.new("Folder")
HiddenFolder.Name = "HiddenFolder"
HiddenFolder.Parent = Player
local HasItem = Instance.new("BoolValue")
HasItem.Name = "HasItem"
HasItem.Value = false
HasItem.Parent = HiddenFolder
So our code so far is this:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Value = 0
Clicks.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 2000000000
Coins.Parent = leaderstats
local HiddenFolder = Instance.new("Folder")
HiddenFolder.Name = "HiddenFolder"
HiddenFolder.Parent = Player
local HasItem = Instance.new("BoolValue")
HasItem.Name = "HasItem"
HasItem.Value = false
HasItem.Parent = HiddenFolder
end)
Now our next challenge is to save and load the data. So here is how we will do it:
First, we will load the data. So inside the playeradded event:
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(Player.UserId)
end)
if success and data then
HasItem.Value = data.HasItem
else
print("There was an error whilst loading the player's data!")
warn(errormessage)
end
And now, when the player leaves the game, we will load that saved data. So inside the same script, player removing event:
game.Players.PlayerRemoving:Connect(function(Player)
local success, errormessage = pcall(function()
local data = {
HasItem = Player.HiddenFolder.HasItem.Value;
}
DataStore:SetAsync(Player.UserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error whilst saving the player's data!")
warn(errormessage)
end
end)
So this is our whole script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Value = 0
Clicks.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 2000000000
Coins.Parent = leaderstats
local HiddenFolder = Instance.new("Folder")
HiddenFolder.Name = "HiddenFolder"
HiddenFolder.Parent = Player
local HasItem = Instance.new("BoolValue")
HasItem.Name = "HasItem"
HasItem.Value = false
HasItem.Parent = HiddenFolder
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(Player.UserId)
end)
if success and data then
HasItem.Value = data.HasItem
else
print("There was an error whilst loading the player's data!")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, errormessage = pcall(function()
local data = {
HasItem = Player.HiddenFolder.HasItem.Value;
}
DataStore:SetAsync(Player.UserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error whilst saving the player's data!")
warn(errormessage)
end
end)
Didn't try this script though, but hopefully it will/should work.
3 comments
.GrayMouse.HasItem didn't you mean .GrayMouse.HasItem.Value?
Also, you didn't retrieve the value from the datastore.
You are using SetAsync(). But where is GetAsync()?