I’m not sure what to do here. I’m trying to create a “wins” datastore. I’ve did this once in a test project, and it works there, but it does not work here, even if I directly copy over the code from the test game. The print(“player added”) part doesn’t even run
local DataStoreService = game:GetService("DataStoreService")
local WinsAndStuff = DataStoreService:GetDataStore("WinsAndStuff")
game.Players.PlayerAdded:Connect(function(player)
print("player added")
local leaderstats = player.leaderstats
local Wins = Instance.new("IntValue")
print("Valued")
Wins.Name = "Wins"
print("Named")
Wins.Parent = leaderstats
print("Adopted")
print("Does this part run?")
local playerUserID = "Player_"..player.UserId
-- Loading data
local data
local success, errormessage = pcall(function()
if data then
Wins.Value = data.Wins
end
end)
game.ReplicatedStorage.InRound.Changed:Connect(function()
local data = player.leaderstats.Wins.Value
local success, errormessage = pcall(function()
WinsAndStuff:SetAsync(playerUserID, data)
end)
if success then
print("Successfully saved to "..playerUserID)
else
print("Error saving")
warn(errormessage)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = player.leaderstats.Wins.Value
local playerUserID = "Player_"..player.UserId
local success, errormessage = pcall(function()
WinsAndStuff:SetAsync(playerUserID, data)
end)
if success then
print("Successfully saved to "..playerUserID)
else
print("Error saving")
warn(errormessage)
end
end)
Ah, you can tell I didn’t get very far down the script before I stopped :-). You need to create the leaderstats folder in the same way you create the values so:
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
ok. So it might not exist before this script event starts, so you could change it to wait for the folder to be added from the other script:
local leaderstats = player:WaitForChild("leaderstats")
That will make it wait for a max of 8 seconds I believe, which should be plenty of time for the other script to create the folder if it is initiated via the same event.
Check if you have studio api access off. If it’s off, the script will error and never run the code. Go to file → game settings → security and turn it on and it should work properly.
studio api access doesnt affect playeradded
the print statement would still run, but the datastore would return an error
In this case, the print statement didnt run, signifying that playeradded was never triggered
@SomieStuff could you show us all the errors you have been getting from that script?
You can get them by going to view - output
turn that on and paste what you see there (minus your personal stuff) into here
Just a quick note: it waits for 5 seconds. If the child doesn’t appear in that time, it prints a warning, but it will never stop waiting. You can optionally specify a timeout, when it will return nil:
foo:WaitForChild("Bar", 10)
-- ^^ will return `nil` OR the child if it's
-- added in the next 10 sec
By extension, you can disable the warning by adding an unusually high number, such as Infinity:
I always assume PlayerAdded may not fire on the first join, because the script might not connect the event before a player is in the game. Make the function you’re connecting to PlayerAdded an actual local function variable, and call that function on all existing players before you :Connect() that signal.