Hello, I want to add Data Store to this script so that the “Jumps” on the leaderstats save so the player can have the same number of jumps when it joins a new server.
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local jumpCount = Instance.new("IntValue")
jumpCount.Name = "Jumps"
jumpCount.Parent = leaderstats
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local debounce = true
humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
if debounce == true then
debounce = false
if humanoid.Jump == true then
jumpCount.Value = jumpCount.Value + 1
end
wait(0.2)
debounce = true
end
end)
end)
end)
The script is in ServerScriptsService and its name is Jumps if that can help
I’ve tried doing something like this do add Data Store:
local DataStoreService = game:GetService("DataStoreService")
local jumpsDatastore = DataStoreService:GetDataStore("JumpsDataStore")
local function loadData(datastore, key)
local success, data = pcall(function()
return datastore:GetAsync(key)
end
return data
end
local function saveData(datastore, key, value)
local success, err = pcall(function() -- "pcall stands for protected call"
datastore:SetAsync(key, value)
end)
if err then print(err) end
end
local jumpCount = Instance.new("IntValue")
jumpCount.Name = "Jumps"
jumpCount.Parent = leaderstats
jumpCount.Value = loadData(jumpsDatastore, player.UserId) or 0
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
game.Players.PlayerRemoving:Connect(function(player)
local jumps = player.leaderstats.Jumps
saveData(jumpsDatastore, player.UserId, jumps.Value)
end
end
and now it gives me the following errors:
I have no clue on how to fix it as I’m not a very skilled scripter and I never tried to add Data Store to a script, any help is appreciated.
Thanks in advance
Seems to just be a few missing brackets at the end of end at a few points. Here are the ones I can find:
Issue one
local function loadData(datastore, key)
local success, data = pcall(function()
return datastore:GetAsync(key)
end
return data
end
should be
local function loadData(datastore, key)
local success, data = pcall(function()
return datastore:GetAsync(key)
end)
return data
end
Issue two
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
game.Players.PlayerRemoving:Connect(function(player)
local jumps = player.leaderstats.Jumps
saveData(jumpsDatastore, player.UserId, jumps.Value)
end
end
should be
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end)
game.Players.PlayerRemoving:Connect(function(player)--Player removing shouldn't be in another function
local jumps = player.leaderstats.Jumps
saveData(jumpsDatastore, player.UserId, jumps.Value)
end)
local DataStoreService = game:GetService("DataStoreService")
local jumpsDatastore = DataStoreService:GetDataStore("JumpsDataStore")
local function loadData(datastore, key)
local success, data = pcall(function()
return datastore:GetAsync(key)
end)
return data
end
local function saveData(datastore, key, value)
local success, err = pcall(function() -- "pcall stands for protected call"
datastore:SetAsync(key, value)
end)
if err then print(err) end
end
local jumpCount = Instance.new("IntValue")
jumpCount.Name = "Jumps"
jumpCount.Parent = leaderstats
jumpCount.Value = loadData(jumpsDatastore, player.UserId) or 0
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end)
game.Players.PlayerRemoving:Connect(function(player)
local jumps = player.leaderstats.Jumps
saveData(jumpsDatastore, player.UserId, jumps.Value)
end)
here is a modified script that should resolve that error:
Script
local DataStoreService = game:GetService("DataStoreService")
local jumpsDatastore = DataStoreService:GetDataStore("JumpsDataStore")
local function loadData(datastore, key)
local success, data = pcall(function()
return datastore:GetAsync(key)
end)
return data
end
local function saveData(datastore, key, value)
local success, err = pcall(function() -- "pcall stands for protected call"
datastore:SetAsync(key, value)
end)
if err then print(err) end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local jumpCount = Instance.new("IntValue")
jumpCount.Name = "Jumps"
jumpCount.Parent = leaderstats
jumpCount.Value = loadData(jumpsDatastore, player.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(player)
local jumps = player.leaderstats.Jumps
saveData(jumpsDatastore, player.UserId, jumps.Value)
end)
I think its because the script I was modifying was missing the code that increased the jump count.
Try this:
Script
local DataStoreService = game:GetService("DataStoreService")
local jumpsDatastore = DataStoreService:GetDataStore("JumpsDataStore")
local function loadData(datastore, key)
local success, data = pcall(function()
return datastore:GetAsync(key)
end)
return data
end
local function saveData(datastore, key, value)
local success, err = pcall(function() -- "pcall stands for protected call"
datastore:SetAsync(key, value)
end)
if err then print(err) end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local jumpCount = Instance.new("IntValue")
jumpCount.Name = "Jumps"
jumpCount.Parent = leaderstats
jumpCount.Value = loadData(jumpsDatastore, player.UserId) or 0
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local debounce = true
humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
if debounce == true then
debounce = false
if humanoid.Jump == true then
jumpCount.Value = jumpCount.Value + 1
end
wait(0.2)
debounce = true
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local jumps = player.leaderstats.Jumps
saveData(jumpsDatastore, player.UserId, jumps.Value)
end)