#DataStore doesn't work

10 messages · Page 1 of 1 (latest)

compact isle
#

When i tried putting datastore in my game, it just gives 3 errors ('kills is not a valid member of Folder "Players.Player2.leaderstats"', ' Script 'ServerScriptService.Leaderboard', Line 45 - function saveData', and 'Script 'ServerScriptService.Leaderboard', Line 57'). Idk how to fix it, since I havent seen any typos. Thoughts?
BTW here's the script:
local DataStore = game:GetService("DataStoreService")
local PlrDataStore = DataStore:GetDataStore("PlayerData")

local function onPlayerJoin(player)
local leaderstats = Instance.new('Folder') --creates a new folder
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player

local kills = Instance.new('IntValue') --creates int value
kills.Name = "Kills" --name of the kills
kills.Value = 0 --its value
kills.Parent = leaderstats --parent of kills (same goes for deaths)

local deaths = Instance.new('IntValue')
deaths.Name = "Deaths"
deaths.Value = 0
deaths.Parent = leaderstats

local killer = Instance.new('StringValue') --makes a killer tag for the player
killer.Name = 'Killer'
killer.Parent = player

local PlayerData

local Success, ErrorMsg = pcall(function()
    local PlayerId = player.UserId
    PlayerData = PlrDataStore:GetAsync(PlayerId)
end)

if Success then
    print("success data load")
    if PlayerData then
        kills.Value = PlayerData[1]
        deaths.Value = PlayerData[2]
    end
else
    warn(ErrorMsg)
end

end

game.Players.PlayerAdded:Connect(onPlayerJoin) --when a player joins the game

#

local function saveData(player)
local Leaderstats = player.leaderstats
local PlayerData = {Leaderstats.kills.Value, Leaderstats.deaths.Value}

local Success, ErrorMsg = pcall(function()
    PlrDataStore:SetAsync(player.UserId, PlayerData)
end)

if not Success then
    warn(ErrorMsg)
end

end

game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)

game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)

#

--Death Tracker

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character) --character gets added to the player
character:WaitForChild('Humanoid').Died:Connect(function() --waiting for the humanoid object to load in
drop_ammo(character.HumanoidRootPart.Position) --identifies position where humanoid dies for ammo drop
drop_health(character.HumanoidRootPart.Position) --identifies position where humanoid dies for health drop
player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
local killerTag = game.Players:FindFirstChild(player.Killer.Value) --looks for player folder, finds player who died, locate killer tag + find value from it
if killerTag then
killerTag.leaderstats.Kills.Value = killerTag.leaderstats.Kills.Value + 1
end
end)
end)
end)

frigid robin
#

please format ur code

hazy mountain
#
--Death Tracker

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character) --character gets added to the player
        character:WaitForChild('Humanoid').Died:Connect(function() --waiting for the humanoid object to load in
            drop_ammo(character.HumanoidRootPart.Position) --identifies position where humanoid dies for ammo drop
            drop_health(character.HumanoidRootPart.Position) --identifies position where humanoid dies for health drop
            player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
            local killerTag = game.Players:FindFirstChild(player.Killer.Value) --looks for player folder, finds player who died, locate killer tag + find value from it
            if killerTag then
                killerTag.leaderstats.Kills.Value = killerTag.leaderstats.Kills.Value + 1
            end
        end)
    end)
end)
#

Hm

#

So based on the errors

#

I would guess u made a typo for getting kills from the leaderstats

#

Maybe check the index name again

hallow ember