Roblox script help
Budget: $10 – $11 USD
the script isnt saving the updates when i kill someone on roblox
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
----Start Kill for cash ----
game.Players.PlayerAdded:Connect (function(player)
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function(Died)
local creator = Character.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
-- if start--
if creator ~= nil and creator.Value ~= nil then
-- what you get when you kill a player--
leaderstats.Humanity.Value = leaderstats.Humanity.Value +1
leaderstats.Coins.Value = leaderstats.Coins.Value +1
leaderstats.Raidbux.Value = leaderstats.Raidbux.Value +1
leaderstats.XP.Value = leaderstats.XP.Value +1
end
-- if end --
end)
end)
end)
---- end kill for cash ----
---- Leaderboards Start----
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local rhumanity = Instance.new("IntValue") --Sets up value for humanity
rhumanity.Name = "Humanity"
rhumanity.Parent = leaderstats
local rcoin = Instance.new("IntValue") --Sets up value for coins
rcoin.Name = "Coins"
rcoin.Parent = leaderstats
local rbux = Instance.new("IntValue") --Sets up value for raidbux
rbux.Name = "Raidbux"
rbux.Parent = leaderstats
local rxp = Instance.new("IntValue") --Sets up value for xp
rxp.Name = "XP"
rxp.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- if player has played before then it will load the data--
rhumanity.Value = data['Humanity']
rcoin.Value = data['Coins']
rbux.Value = data['Raidbux']
rxp.Value = data['XP']
else
-- no players start with this default--
rhumanity.Value = 1
rcoin.Value = 100
rbux.Value = 0
rxp.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) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
---- Leaderboards End ----
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
----Start Kill for cash ----
game.Players.PlayerAdded:Connect (function(player)
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function(Died)
local creator = Character.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
-- if start--
if creator ~= nil and creator.Value ~= nil then
-- what you get when you kill a player--
leaderstats.Humanity.Value = leaderstats.Humanity.Value +1
leaderstats.Coins.Value = leaderstats.Coins.Value +1
leaderstats.Raidbux.Value = leaderstats.Raidbux.Value +1
leaderstats.XP.Value = leaderstats.XP.Value +1
end
-- if end --
end)
end)
end)
---- end kill for cash ----
---- Leaderboards Start----
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local rhumanity = Instance.new("IntValue") --Sets up value for humanity
rhumanity.Name = "Humanity"
rhumanity.Parent = leaderstats
local rcoin = Instance.new("IntValue") --Sets up value for coins
rcoin.Name = "Coins"
rcoin.Parent = leaderstats
local rbux = Instance.new("IntValue") --Sets up value for raidbux
rbux.Name = "Raidbux"
rbux.Parent = leaderstats
local rxp = Instance.new("IntValue") --Sets up value for xp
rxp.Name = "XP"
rxp.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
-- if player has played before then it will load the data--
rhumanity.Value = data['Humanity']
rcoin.Value = data['Coins']
rbux.Value = data['Raidbux']
rxp.Value = data['XP']
else
-- no players start with this default--
rhumanity.Value = 1
rcoin.Value = 100
rbux.Value = 0
rxp.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) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
---- Leaderboards End ----