recently i have made a leaderstats folder for my upcoming game. When i try to access this from another script it prints out “unable to index nil with leaderstats”, any help. I know that i have spelt leaderstats right because i have checked both scripts.
i will try using PlayerAdded and i am using this in a server script. To give this more detail what i want to do is when a player touches a part it adds 100 Cash to a value and then saves it if that helps. I will try it now.
local Part = script.Parent
local Amount = 100
Part.Touched:Connect(function(Hit)
if game.Players:GetPlayerFromCharacter(Hit.Parent) then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
Player:WaitForChild("leaderstats").Cash.Value = Player:WaitForChild("leaderstats").Cash.Value + Amount
end
end)
Try putting this inside your brick you want the player to touch and get cash.
the odd thing is is when i join the game and i look into the leaderstats at the top playerlist, it says the name is “Value” but when i check the explorer it says the value is bloks (the currency name)
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Bloks")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local bloks = Instance.new("IntValue")
bloks.Value = 0
bloks.Name = "bloks"
bloks.Parent = leaderstats
if not RunService:IsStudio() then
local data
local success, err = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
if success then
if data then
bloks.Value = data
end
else
player:Kick("Failed to load data, please rejoin")
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
DataStore:SetAsync(player.UserId, player.leaderstats.bloks.Value)
end)
end)