'm trying to do this to save leaderstats through each game inside one starterplace, like with levels and KO’s.
Here’s my code right now and it doesn’t work:
local DataStoreService = game:GetService("DataStoreService")
local Credits = DataStoreService:GetDataStore("Credits")
local Wins = DataStoreService:GetDataStore("Wins")
local XP = DataStoreService:GetDataStore("XP")
local Level = DataStoreService:GetDataStore("Level")
local Hats = DataStoreService:GetDataStore("Hats")
local http = game.HttpService
local url = "http://www.google.com"
local saliva = http:GetAsync(url, true)
print(saliva)
print()
--Capitilization is important!
game.Players.PlayerAdded:connect(function(plr)
local key = "user_"..plr.userId -- UserId's are unique and stay the same for each player so that's why I am using this (it will also account for name changes)
local stats = Instance.new("IntValue",plr)
stats.Name = "leaderstats"
local credits = Instance.new("IntValue",stats)
credits.Name = "Credits"
credits.Value = Credits:GetAsync(key)
local wins = Instance.new("IntValue",stats)
wins.Name = "Wins"
wins.Value = Wins:GetAsync(key)
wins.Value = Wins:GetAsync(url)
local level = Instance.new("IntValue",stats)
level.Name = "Level"
--level.Value = Level:GetAsync(key,url)
level.Value = Level:GetAsync(url)
local hats = Instance.new("IntValue",stats)
hats.Name = "Hats"
hats.Value = Hats:GetAsync(key)
local xp = Instance.new("IntValue",stats)
xp.Name = "XP"
xp.Value = XP:GetAsync(key)
function onXPChanged(plr, xp, level)
if xp.Value >= 30 then
level.Value = level.Value + 1
xp.Value = 0
end
end
function onWinsChanged(plr, wins)
--if Tags.Value == Tags.Value + 1 then
local q = script.Kills:Clone()
q.Parent = plr.PlayerGui
script.click:Play()
end
credits.Changed:connect(function() Credits:SetAsync(key, credits.Value) end) -- These save the level and money when their stats update
wins.Changed:connect(function() Wins:SetAsync(key, wins.Value) end)
xp.Changed:connect(function() XP:SetAsync(key, xp.Value) end)
xp.Changed:connect(function() onXPChanged(plr, xp, credits) end)
xp.Changed:connect(function() onXPChanged(plr, xp, level) end)
wins.Changed:connect(function() onWinsChanged(plr, wins) end)
level.Changed:connect(function() Level:GetAsync(url, level.Value) end)
hats.Changed:connect(function() Hats:SetAsync(key, hats.Value) end)
--[[while true do
if plr.Character ~= nil then break end
wait(5)
end
local humanoid = plr.Character.Humanoid
humanoid.Died:connect(function() onHumanoidDied(humanoid, plr) end )
-- start to listen for new humanoid
plr.Changed:connect(function(property) onPlayerRespawn(property, plr) end )
stats.Parent = plr
end)
]]
end)
function Send_DB_Event_Died(victim, killer)
-- killer may be nil
local killername = "unknown"
if killer ~= nil then killername = killer.Name end
print(victim.Name, " was killed by ", killername)
if shared["deaths"] ~= nil then
shared["deaths"](victim, killer)
print("Death event sent.")
end
end
function Send_DB_Event_Kill(killer, victim)
print(killer.Name, " killed ", victim.Name)
if shared["kills"] ~= nil then
shared["kills"](killer, victim)
print("Kill event sent.")
end
end
function onHumanoidDied(humanoid, player)
local stats = player:findFirstChild("leaderstats")
if stats ~= nil then
--local deaths = stats:findFirstChild("Tags")
local xp = stats:findFirstChild("XP")
--deaths.Value = deaths.Value + 1
--xp.Value = xp.Value + 1
local killer = getKillerOfHumanoidIfStillInGame(humanoid)
Send_DB_Event_Died(player, killer)
handleKillCount(humanoid, player)
end
end
function onPlayerRespawn(property, player)
-- need to connect to new humanoid
if property == "Character" and player.Character ~= nil then
local humanoid = player.Character.Humanoid
local p = player
local h = humanoid
humanoid.Died:connect(function() onHumanoidDied(h, p) end )
end
end
function getKillerOfHumanoidIfStillInGame(humanoid)
-- returns the player object that killed this humanoid
-- returns nil if the killer is no longer in the game
-- check for kill tag on humanoid - may be more than one - todo: deal with this
local tag = humanoid:findFirstChild("creator")
-- find player with name on tag
if tag ~= nil then
local killer = tag.Value
if killer.Parent ~= nil then -- killer still in game
return killer
end
end
return nil
end
function handleKillCount(humanoid, player)
local killer = getKillerOfHumanoidIfStillInGame(humanoid)
if killer ~= nil then
local stats = killer:findFirstChild("leaderstats")
if stats ~= nil then
local kills = stats:findFirstChild("Tags")
local xp = stats:findFirstChild("XP")
if killer ~= player then
kills.Value = kills.Value + 1
xp.Value = xp.Value + 3
wait(2)
player.PlayerGui.Kills:Destroy()
else
kills.Value = kills.Value - 0
end
Send_DB_Event_Kill(killer, player)
end
end
end
I’ve been asking a lot of questions today on this forum, so sorry about all that!
Can you even save leaderstats with httpservice? Please help! Thanks!
Oh BTW the “Level” leaderstat is the only one I’ve coded for the httpservice.