Could somebody tell me why i keep getting an error called attempt to index nil when identifying the leaderstats heres the code,
You cant use game.Players.LocalPlayer in Server Scripts, instead use game.Players.PlayerAdded to add the leaderstats folder when a player joins the game, and also wins is a folder which doesnt have the value property, instead use NumberValues to store numbers.
game.Players.PlayerAdded:Connect(function(NewPlayer)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = NewPlayer
local Wins = Instance.new("NumberValue")
Wins.Parent = leaderstats
Wins.Name = "Wins"
Wins.Value = 1000
end)
Can you zoom in? its hard to read the script
Doesnt appear to be the case. Looking at it, there i ls no LocalPlayer
besides in the player added function which is normal.
Edit: noticed the script above
i already have a leaderstats i want to identify that same leaderstat and add another value to it because add it to the main script doesnt work
You can also use game.Players.PlayerAdded for that,
game.Players.PlayerAdded:Connect(function(NewPlayer)
local leaderstats = NewPlayer:WaitForChild("leaderstats")
local Wins = Instance.new("NumberValue")
Wins.Parent = leaderstats
Wins.Name = "Wins"
Wins.Value = 1000
end)
Im not sure why you need 2 Scripts for the same thing, just do this:
DS = game:GetService("DataStoreService"):GetDataStore("ExampleData")
game.Players.PlayerAdded:Connect(function(Plr)
local ls = Instance.new("Folder", Plr)
ls.Name = "leaderstats"
local w = Instance.new("IntValue", ls)
w.Name = "Wins"
w.Value = DS:GetAsync(Plr.UserId, w.Value) or 1000
w:GetPropertyChangedSignal("Value"):Connect(function()
DS:UpdateAsync(Plr.UserId, w.Value)
end
end
game.Players.PlayerRemoving:Connect(function(Plr)
DS:SetAsync(Plr.UserId, Plr.Wins.Value)
end)
If you want to get the value and put it inside a Gui:
LocalScript:
while task.wait() do
Gui.TextLabel.Text = game.Players.LocalPlayer:FindFirstChild("leaderstats").Wins.Value
end
To add a New Value:
DS = game:GetService("DataStoreService"):GetDataStore("ExampleData")
game.Players.PlayerAdded:Connect(function(Plr)
local ls = Instance.new("Folder", Plr)
ls.Name = "leaderstats"
local w = Instance.new("IntValue", ls)
w.Name = "Wins"
w.Value = DS:GetAsync(Plr.UserId, w.Value) or 1000
--/Our New Value/--
local NewValue = Instance.new("IntValue", Plr)
NewValue.Name = "ExampleValue"
NewValue.Value = w
----
w:GetPropertyChangedSignal("Value"):Connect(function()
DS:UpdateAsync(Plr.UserId, w.Value)
end
end
game.Players.PlayerRemoving:Connect(function(Plr)
DS:SetAsync(Plr.UserId, Plr.Wins.Value)
end)
Thank you this worked but do you mind adding a data store to this?
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.