local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')
-- Erstellt Leaderboard
local function onPlayerJoin(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 rebirths = Instance.new('IntValue')
rebirths.Name = 'Rebirths'
rebirths.Value = 0
rebirths.Parent = leaderstats
-- Wenn ein Nutzer verlassen hat das die daten gespeichert wurden
local playerUserId = 'Player_'..player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
clicks.Value = data['Clicks']
rebirths.Value = data['Rebirths']
else -- Wenn user das erste mal joint das value 0 ist
clicks.Value = 0
rebirths.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function ()
local playerUserId = 'Player_'..player.UserId
playerData:SetAsync(playerUserId, player_stats)
end)
if not success then
warn('Could not save data')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
I tried to make a DataStore that stores the values of the leaderboard into a data that returns everytime if i rejoin. But it doesnt work and idk why. Im kinda new into lua programming but u guys can help me :D thanks.
Related
I am trying to make a leader board that tells the player how much cats they have collected. the player has a file and the script counts how many items the file has then displays it on the leaderboard, but it doesn't work(shows the leaderboard but no second row for the amount collected)! Any ideas??
here is my code:
`local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder", player)
folder.Name = "leaderstats"
local catsamount = Instance.new("IntValue", folder)
catsamount.Name = "Cats Found"
catsamount.Value = 0
while wait(0.1) do
catsamount.Value = 0
for _, v in pairs(catscollected:GetChildren()) do
catsamount.Value = catsamount.Value + 1
end
end
end)`
You have to parent catsamount to folder and folder to player:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local catsamount = Instance.new("IntValue", folder)
catsamount.Name = "Cats Found"
catsamount.Value = 0
catsamount.Parent = folder
while wait(0.1) do
catsamount.Value = 0
for _, v in pairs(catscollected:GetChildren()) do
catsamount.Value = catsamount.Value + 1
end
end
end)
This is so that the folder will be added to the player after it is created - Instance.new() creates an instance, but it does not parent the created instance to anything so it will not appear anywhere.
I was making a game, with a status system that includes some points to give away. I used roblox's DataStore service, but I'm having a problem with my game. After a player spends his points, distributing them in the stats he wants, after he leaves the game and re-enters those same points back to him with the same value, this can cause a point doubling glitch, which makes the unfair game, which I don't want. I think there is an error in my code, what can I do to fix it?
Code below:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlrData")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(Plr)
local data = Instance.new("Folder", Plr)
data.Name = "Data"
local exp = Instance.new("IntValue", data)
exp.Name = "Exp"
exp.Value = 1
local levels = Instance.new("IntValue", data)
levels.Name = "Level"
levels.Value = 1
local Points = Instance.new("IntValue", data)
Points.Name = "Points"
Points.Value = 3
local expneed = Instance.new("IntValue", data)
expneed.Name = "ExpNeeded"
expneed.Value = 100
local strenght = Instance.new("IntValue", data)
strenght.Value = 1
strenght.Name = "Strenght"
local Health = Instance.new("IntValue", data)
Health.Value = 100
Health.Name = "Health"
local char = Plr.Character or Plr.CharacterAdded:Wait()
local charhealth = char.Humanoid.Health
local DF = Instance.new("IntValue", data)
DF.Value = 1
DF.Name = "DF"
local PlayerId = Plr.UserId
local PlayerData = DataStore:GetAsync(PlayerId)
if PlayerData then
strenght.Value = PlayerData["Strenght"]
Health.Value = PlayerData["Health"]
DF.Value = PlayerData["DF"]
exp.Value = PlayerData["Exp"]
levels.Value = PlayerData["Level"]
expneed.Value = PlayerData["ExpNeeded"]
Points.Value = PlayerData["Points"]
charhealth = PlayerData["Health"]
end
end)
local function tab(Player)
local PlrStats = {}
for _, v in pairs(Player.Data:GetChildren()) do
PlrStats[v.Name] = v.Value
end
return PlrStats
end
game.Players.PlayerRemoving:Connect(function(Plr)
local PlrStats = tab(Plr)
local Sucess, Result = pcall(function()
local PlrId = Plr.UserId
DataStore:SetAsync(PlrId, PlrStats)
end)
if not Sucess then
print(Result)
warn("Error save")
end
end)
game:BindToClose(function()
for _, Player in pairs(game.Players:GetPlayers()) do
task.wait()
local PlrStats = tab(Player)
local Sucess, Result = pcall(function()
local PlrId = Player.UserId
DataStore:SetAsync(PlrId, PlrStats)
end)
if not Sucess then
print(Result)
warn("Error")
end
end
end)
game.Players.PlayerAdded:Connect(function(Player)
task.wait(0.1)
local Level = Player.Data.Level
local Exp = Player.Data.Exp
local ExpNeed = Player.Data.ExpNeeded
local Points = Player.Data.Points
local Strenght = Player.Data.Strenght
local Health = Player.Data.Health
local DF = Player.Data:WaitForChild("DF")
RunService.Heartbeat:Connect(function()
if Exp.Value == 0 and ExpNeed.Value == 0 then
Exp.Value = 1
ExpNeed.Value = 100
end
if Points.Value > 300 then
Points.Value = 300
end
if Exp.Value >= ExpNeed.Value then
Exp.Value = Exp.Value - ExpNeed.Value
Level.Value += 1
ExpNeed.Value *= 1.13
Points.Value += 3
end
if Strenght.Value == 0 or DF.Value == 0 or Health.Value == 0 then
Strenght.Value = 1
DF.Value = 1
Health.Value = 1
end
if Strenght.Value > 100 or DF.Value > 100 then
Strenght.Value = 100
DF.Value = 100
end
if Health.Value > 1000 then
Health.Value = 1000
end
end)
RunService.Heartbeat:Connect(function()
if Level.Value >= 100 then
Level.Value = 100
end
end)
end)
here it is, it is roblox code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AddStats")
local player = game:GetService("Players")
local leaderstats = player:WaitForChild('leaderstats', 10)
local clicks = leaderstats.Clicks
local rebirths = leaderstats.Rebirths
local function clicksStore()
clicks.Value = clicks.Value + 1
if rebirths <= 1 then
clicks.Value = clicks.Value + rebirths.Value
end
end
remoteEvent.OnServerEvent:Connect(clicksStore)
is there anyway i can fix this? its getting on my nerves. Thank you for helping!
You are indexing the leaderstats correctly, but in a server Script, you aren't accessing the player correctly.
You can easily access the player by using the RemoteEvent's OnServerEvent connection.
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AddStats")
local function clicksStore(player)
local leaderstats = player.leaderstats
local clicks = leaderstats.Clicks
local rebirths = leaderstats.Rebirths
local clickValue = 1 + rebirths.Value
clicks.Value += clickValue
end
remoteEvent.OnServerEvent:Connect(clicksStore)
I tried to make the functions via Datastore so that it only saved the data Wins and reset the stage always but saved Wins always so if I have 250 stages and 1 win and I leave the game I will have 0 stages and 1 win.
local CheckpointService = {}
-- SERVICES
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
-- MODULES
local AchievementService = require(game.ServerScriptService.Server:WaitForChild("Achievement"))
-- VARIABLES
local CHECKPOINTS = game.Workspace:WaitForChild("Checkpoints")
local STAT_NAME = "Stage"
local WINS_NAME = "Wins"
local PREVENT_SKIPPING = false
-- CONNECTIONS
local function playerAdded(player)
local leaderstats = Instance.new("Folder", player) or player:FindFirstChild("leaderstats")
leaderstats.Name = "leaderstats"
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = STAT_NAME
checkpointStat.Value = 1
local checkpointStat = Instance.new("IntValue", leaderstats)
checkpointStat.Name = WINS_NAME
checkpointStat.Value = 0
player.CharacterAdded:connect(function(character)
repeat
wait()
until CHECKPOINTS and checkpointStat
local goto = CHECKPOINTS:FindFirstChild("Checkpoint " .. checkpointStat.Value)
if goto then
character:MoveTo(goto.Position)
else
warn("[SERVER] - Checkpoint " .. checkpointStat.Value .. " not found!")
end
end)
end
When I kill an NPC, my score should increase, but it does not.
Here's my code. It is located under my NPC model.
local Humanoid = script.Parent.Humanoid
function PwntX_X()
local tag = Humanoid:findFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local Leaderstats = tag.Value:findFirstChild("leaderstats")
if Leaderstats ~= nil then
Leaderstats.Score.Value = Leaderstats.Score.Value + 250 --Change Money to the stat that is increased.
wait(0.1)
script:remove()
end
end
end
end
Humanoid.Died:connect(PwntX_X)
And here's my code for the leaderboard
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue",plr)
stats.Name = "leaderstats"
local score = Instance.new("IntValue", stats)
score.Name = "Score"
score.Value = 0
end)
Not sure, but looks like
score.Value = Leaderstats.Score.Value