If you still don’t understand I want to be-able to fire/call a function and run the code underneath it instantly.
Code:
if DatastoreDucks:GetAsync("-ducks") then
createDucks(Player, DatastoreDucks:GetAsync("-ducks"))
else
print(Player.Name.." is a new player. Thanks for joining!")
end
if DatastoreDucks:GetAsync("-leaderstats") then
stars.Value = DatastoreDucks:GetAsync("-leaderstats")
game.Workspace.Baseplate:Destroy()
else
game.Workspace.Baseplate.BrickColor = BrickColor.Green()
end
createDucks(Player, DatastoreDucks:GetAsync("-ducks"))
This function has a loop that will run forever and so it won’t continue on with the script.
All of the code:
local DataStore = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DatastoreDucks = DataStore:GetDataStore("ftDucks")
local function saveData(Player, duckArray)
pcall(function()
print("Saving...")
DatastoreDucks:SetAsync("-ducks", duckArray)
print("Saved duckstats")
DatastoreDucks:SetAsync("-leaderstats", Player:FindFirstChild("leaderstats"):FindFirstChild("Stars").Value)
print("Saved leaderstats")
end)
end
local function autoSaveText(Player)
local AutoSaveText = Player.PlayerGui:FindFirstChild("MainDuck"):FindFirstChild("Autosave")
local DotAmount = {".", "..", "..."}
AutoSaveText.Visible = true
for i = 1, 3 do
wait(0.85)
AutoSaveText.Text = "Autosaving"..DotAmount[i]..""
if i == 3 then
wait(2)
AutoSaveText.Text = "Saved data."
AutoSaveText.TextColor3 = Color3.fromRGB(255, 255, 127)
wait(2)
AutoSaveText.Visible = false
AutoSaveText.Text = "Autosaving"
AutoSaveText.TextColor3 = Color3.fromRGB(0, 0, 0)
end
end
end
local function autoSave(Player, duckFolder)
while true do
task.wait(10)
if #duckFolder:GetChildren() < 1 then
print(Player.Name.." does not have any ducks unlocked! :(")
else
local duckArray = {}
for _,duck in pairs(duckFolder:GetChildren()) do
table.insert(duckArray, duck.Name)
end
autoSaveText(Player)
saveData(Player, duckArray)
end
end
end
local function createDucks(Player, DuckArray)
local duckFolder = Player:FindFirstChild("Ducks")
for i = 1, #DuckArray do
local newDuck = Instance.new("BoolValue")
newDuck.Name = DuckArray[i]
newDuck.Value = true
newDuck.Parent = Player:FindFirstChild("Ducks")
end
autoSave(Player, duckFolder)
end
Players.PlayerAdded:Connect(function(Player)
pcall(function()
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local stars = Instance.new("IntValue")
stars.Name = "Stars"
stars.Value = 0
stars.Parent = leaderstats
local duckFolder = Instance.new("Folder")
duckFolder.Name = "Ducks"
duckFolder.Parent = Player
if DatastoreDucks:GetAsync("-ducks") then
createDucks(Player, DatastoreDucks:GetAsync("-ducks"))
else
print(Player.Name.." is a new player. Thanks for joining!")
end
if DatastoreDucks:GetAsync("-leaderstats") then
stars.Value = DatastoreDucks:GetAsync("-leaderstats")
game.Workspace.Baseplate:Destroy()
else
game.Workspace.Baseplate.BrickColor = BrickColor.Green()
end
end)
end)
Players.PlayerRemoving:Connect(function(Player)
pcall(function()
local duckFolder = Player:FindFirstChild("Ducks")
if #duckFolder:GetChildren() < 1 then return end
local duckArray = {}
for _,duck in pairs(duckFolder:GetChildren()) do
table.insert(duckArray, duck.Name)
end
saveData(Player, duckArray)
end)
end)
game:BindToClose(function()
wait(3)
end)
Please help me. Thank you!