What do you want to achieve? Keep it simple and clear!
–
What is the issue? Include screenshots / videos if possible!
Data isn’t saving and no errors in output.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
local event = game.ReplicatedStorage:WaitForChild("coin")
local datastore = game:GetService("DataStoreService")
local myData = datastore:GetDataStore("myData")
--local GateData = datastore:GetDataStore("GateData")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local coins = Instance.new("IntValue",leaderstats)
coins.Name = "Coins"
local gate1 = Instance.new("BoolValue", plr)
--gate1.Value = false
gate1.Name = "Gate1"
print("Boolean Status :",gate1.Value)
local data
local success,err = pcall(function()
data = myData:GetAsync(plr.UserId)
end)
if success then
coins.Value = data
--gate1.Value = data
end
local function clockTime()
event:FireClient(plr)
coins.Value += math.random(1,100)
end
while wait(25) do
clockTime()
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success,err = pcall(function()
myData:SetAsync(plr.UserId.."Coins",plr.leaderstats.Coins.Value)
end)
if not success then
warn(err)
end
local success,err = pcall(function()
myData:SetAsync(plr.UserId.."Gate1",plr.Gate1.Value)
end)
if not success then
warn(err)
end
end)
local event = game.ReplicatedStorage:WaitForChild("coin")
local datastore = game:GetService("DataStoreService")
local myData = datastore:GetDataStore("myData")
--local GateData = datastore:GetDataStore("GateData")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local coins = Instance.new("IntValue",leaderstats)
coins.Name = "Coins"
local gate1 = Instance.new("BoolValue", plr)
--gate1.Value = false
gate1.Name = "Gate1"
local success,data = pcall(function() -- I CHANGE THIS
return myData:GetAsync(plr.UserId.."Coins")
end)
if success then
coins.Value = data
--gate1.Value = data
end
local function clockTime()
event:FireClient(plr)
coins.Value += math.random(1,100)
end
while wait(25) do
clockTime()
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success,err = pcall(function()
myData:SetAsync(plr.UserId.."Coins",plr.leaderstats.Coins.Value)
end)
if not success then
warn(err)
end
local success,err = pcall(function()
myData:SetAsync(plr.UserId.."Gate1",plr.Gate1.Value)
end)
if not success then
warn(err)
end
end)
My current theory is you’re using :WaitForChild() which would be fine but it’s taking too long to find the child and by the time it finds it/gives up your player has already loaded but the function hasn’t, so it doesn’t run.
Every 25 seconds, the function is launched along with a remote event. The local script accepts and within 5 seconds the player runs faster than the previous speed.
One last question, why is clocktime() a function? Couldn’t you just put the code in there in the while loop or do you need it as a function for future use?
Not sure what you mean but I know the overall goal so here’s what I came up with.
first get rid of this line at the top:
local event = game.ReplicatedStorage:WaitForChild("coin")
then change
local function clockTime()
game.ReplicatedStorage:WaitForChild("coin"):FireClient(plr)
coins.Value += math.random(1,100)
end
while wait(25) do
clockTime()
end
to
while task.wait(25) do
game.ReplicatedStorage:WaitForChild("coin"):FireClient(plr)
coins.Value += math.random(1,100)
end
This has a bit of a flaw though, we’re waiting 25 seconds AND the amount of time we wait for “coin”. If “coin” actually exists then this isn’t much of an issue because we’re only waiting a very small amount of extra time, but overall waiting can be a very small bit unreliable, if you wanted something more concrete you should look into RunService.
In your original code you have the boolean saving commented out.
Here’s the full script, everything should be working in it.
local datastore = game:GetService("DataStoreService")
local myData = datastore:GetDataStore("myData")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local coins = Instance.new("IntValue",leaderstats)
coins.Name = "Coins"
local gate1 = Instance.new("BoolValue", plr)
gate1.Name = "Gate1"
local success,data = pcall(function() -- I CHANGE THIS
return myData:GetAsync(plr.UserId.."Coins")
end)
if success then
coins.Value = data
end
local success,data = pcall(function() -- I CHANGE THIS
return myData:GetAsync(plr.UserId.."Gate1")
end)
if success then
gate1.Value = data
end
while task.wait(25) do
game.ReplicatedStorage:WaitForChild("coin"):FireClient(plr)
coins.Value += math.random(1,100)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success,err = pcall(function()
myData:SetAsync(plr.UserId.."Coins",plr.leaderstats.Coins.Value)
end)
if not success then
warn(err)
end
local success,err = pcall(function()
myData:SetAsync(plr.UserId.."Gate1",plr.Gate1.Value)
end)
if not success then
warn(err)
end
end)
Double check that studio isn’t causing this issue because sometimes data doesn’t save through studio due to the server closing at the same time as the client when you end a play test.
I’ve recently had this issue and another way to solve it is by running a local server test with just one player and leaving the game but not closing the server and wait for it to save.