That gets rid of the spamming error. But how am I gonna put leaderstats in the player if leaderstats isn’t there?
PlayerAdded event is fired when the player is added to “Players” so technically, there isnt a point where the player isn’t there (and at that point, you want to put leaderstats in the player).
Check my reply again, I edited it.
I am not sure what you mean by this.
If leaderstats wasn’t in the player, then how would I make a new leaderstats inside the player?
I already replied with the following code.
I know how to make leaderstats, but how would I know if leaderstats wasn’t there to instance it again?
You check if it exists:
In a local script:
if not game.Players.LocalPlayer:FindFirstChild("leaderstats") then
-- leaderstats does not exist, your code here
end
If you want to create leaderstats if it does not exist, you can use a remote event. Create a remove event and place it in ReplicatedStorage, and after that - fire it on the client and then address the rest on the server! :
if not game.Players.LocalPlayer:FindFirstChild("leaderstats") then
-- leaderstats does not exist, your code here
someRemoteEvent:Fire()
end
In a script inside server script service:
theRemoveEvent.OnServerEvent:Connect(function(plr) -- the first argument is by default the player
local leaderstats = Instance.new('Folder')
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local coins = Instance.new('IntValue')
coins.Name = 'Coins'
coins.Value = 0
coins.Parent = leaderstats
end)
Would the local script be my local script in my Text Label?
It can be any localscript inside lets say StarterPlayerScripts or StarterGui
Okay, that’ll work. But what would happen if a hacker would call that event?
If the event was called, what would I check for?
Random thing, instead of randomly updating this, try doing it only when leaderstats changes:
plr.leaderstats.Changed:Connect(function()
script.Parent.Text = tostring(plr.leaderstats.Coins.Value)
end)
Also, the problem is probably that your script runs before leaderstats is replicated, try waiting for leaderstats:
local leaderstats = plr:WaitForChild("leaderstats")
leaderstats.Changed:Connect(function()
script.Parent.Text = tostring(leaderstats.Coins.Value)
end)
Another thing, I’d recommend setting the properties before the parent:
local DS2 = require(1936396537)
local DefaultValue = 0
game.Players.PlayerAdded:Connect(function(plr)
local coinsDS = DS2('Coins', plr)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
local coins = Instance.new('IntValue')
coins.Name = 'Coins'
coins.Parent = leaderstats
leaderstats.Parent = plr
local function updateCoins(updatedValue)
coins.Value = coinsDS:Get(updatedValue)
end
updateCoins(DefaultValue) -- Idk what these two lines are supposed to do
coinsDS:OnUpdate(updateCoins) -- I just copied Alvin Blox :|
while wait() do --There is probably a better way to do this XD
coins.Value = coinsDS:Get()
end
end)
Can you write the full script pls?
local plr = game:GetService("Players").LocalPlayer
local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")
coins.Changed:Connect(function()
script.Parent.Text = coins.Value
end)
local DS2 = require(1936396537)
local DefaultValue = 0
game.Players.PlayerAdded:Connect(function(plr)
local coinsDS = DS2('Coins', plr)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
local coins = Instance.new('IntValue')
coins.Name = 'Coins'
coins.Parent = leaderstats
leaderstats.Parent = plr
local function updateCoins(updatedValue)
coins.Value = coinsDS:Get(updatedValue)
end
updateCoins(DefaultValue) -- Idk what these two lines are supposed to do
coinsDS:OnUpdate(updateCoins) -- I just copied Alvin Blox :|
while wait() do --There is probably a better way to do this XD
coins.Value = coinsDS:Get()
end
end)
I said this 4 times already. I want to instance a new leaderstats, if leaderstats isn’t already in the player.
Sorry I missed those posts here:
ReplicatedStorage->CreateLeaderstatsEvent
ServerScriptService->CreateLeaderstatsScript
local event = game.ReplicatedStorage.CreateLeaderstatsEvent
event.OnServerEvent:Connect(function(plr) -- the first argument is by default the player
local leaderstats = Instance.new('Folder')
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local coins = Instance.new('IntValue')
coins.Name = 'Coins'
coins.Value = 0
coins.Parent = leaderstats
end)
local plr = game:GetService("Players").LocalPlayer
local event = game.ReplicatedStorage.CreateLeaderstatsEvent
if not plr:FindFirstChild("leaderstats") then
-- leaderstats does not exist, your code here
event:FireServer()
end
local coins = plr:WaitForChild("leaderstats"):WaitForChild("Coins")
coins.Changed:Connect(function()
script.Parent.Text = coins.Value
end)
local DS2 = require(1936396537)
local DefaultValue = 0
game.Players.PlayerAdded:Connect(function(plr)
local coinsDS = DS2('Coins', plr)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
local coins = Instance.new('IntValue')
coins.Name = 'Coins'
coins.Parent = leaderstats
leaderstats.Parent = plr
local function updateCoins(updatedValue)
coins.Value = coinsDS:Get(updatedValue)
end
updateCoins(DefaultValue) -- Idk what these two lines are supposed to do
coinsDS:OnUpdate(updateCoins) -- I just copied Alvin Blox :|
while wait() do --There is probably a better way to do this XD
coins.Value = coinsDS:Get()
end
end)
How do I prevent hackers from calling the event? Sorry for not replying sooner.
this is running before data store script runs, instead of runservice use this,
local plr = script.Parent.Parent.Parent.Parent.Parent
local leaderstats = plr:WaitForChild("leaderstats")
local coins = leaderstats:WaitForChild("Coins")
script.Parent.Text = coins.Value
coins.Changed:Connect(function()
script.Parent.Text = coins.Value
end)
Also,
This is horrible, you will end up going over the limits and I don’t think you need something like that in DS2.