I am trying to make a player equip a hat when they join if their hat purchased variable is true.
Part of my saving and loading script.
game.Players.PlayerAdded:Connect(function(player) local folder = Instance.new("Folder", player) --Creating variables folder.Name = "leaderstats" local folder2 = Instance.new("Folder", player) folder2.Name = "variables" local folder3 = Instance.new("Folder", player) folder3.Name="purchases" local val = Instance.new("IntValue",folder) val.Name = "Cash" local val2 = Instance.new("IntValue",folder2) val2.Name = "PPC" local val3 = Instance.new("IntValue",folder2) val3.Name = "CPS" local val4 = Instance.new("IntValue",folder2) val4.Name = "Price" local val5 = Instance.new("IntValue",folder2) val5.Name = "CPSPrice" local val6 = Instance.new("BoolValue",folder3) val6.Name = "TopHat" local stats = ds:GetAsync(player.UserId) if stats ~= nil then val.Value = stats[1]--The numbers are the table below val2.Value = stats[2] val3.Value = stats[3] val4.Value = stats[4] val5.Value = stats[5] val6.Value = stats[6] else val.Value = 0 val2.Value = 1 val3.Value = 0 val4.Value = 10 val5.Value = 500 val6.Value = false end
This is what I tried in a separate script This prints No Hat
game.Players.PlayerAdded:Connect(function(player) local purchases = player:WaitForChild("purchases") local TopHat = purchases:WaitForChild("TopHat") if TopHat.Value == true then local pmodel = player.Character local hum = pmodel:WaitForChild("Humanoid") local ss = game:GetService("ServerStorage") local hat = ss:WaitForChild("TopHat"):Clone() hum:RemoveAccessories() hum:AddAccessory(hat) print("Has hat") else print("Has No hat") end end)
This is what I tried in the same script as the one that saves and loads It is in player added Doesn't print at all
if player.purchases.TopHat.Value == true then local pmodel = player.Character local hum = pmodel:WaitForChild("Humanoid") local ss = game:GetService("ServerStorage") local hat = ss:WaitForChild("TopHat"):Clone() hum:RemoveAccessories() hum:AddAccessory(hat) print("Hat") else print("No Hat") end
This is probably because there is no saving datastore. It uses GetAsync() on a datastore that either doesn't exist, or the datastore isn't saving in the first place. Try wrapping the GetAsync with a pcall too. It automatically sees that the datastore hasnt saved anything, so it gives you nil. This is why its saying no hat; the stats = nil. Hopefully this helps.
ds = game:GetService("DataStoreService"):GetDataStore(--put storename here-) game.Players.PlayerAdded:Connect(function(player) local folder = Instance.new("Folder", player) --Creating variables folder.Name = "leaderstats" local folder2 = Instance.new("Folder", player) folder2.Name = "variables" local folder3 = Instance.new("Folder", player) folder3.Name="purchases" local val = Instance.new("IntValue",folder) val.Name = "Cash" local val2 = Instance.new("IntValue",folder2) val2.Name = "PPC" local val3 = Instance.new("IntValue",folder2) val3.Name = "CPS" local val4 = Instance.new("IntValue",folder2) val4.Name = "Price" local val5 = Instance.new("IntValue",folder2) val5.Name = "CPSPrice" local val6 = Instance.new("BoolValue",folder3) val6.Name = "TopHat" local stats local success,errormessage = pcall(function() stats = ds:GetAsync(player.UserId) end) if success then if stats ~= nil then print("Successfully retrieved stats") val.Value = stats[1]--The numbers are the table below val2.Value = stats[2] val3.Value = stats[3] val4.Value = stats[4] val5.Value = stats[5] val6.Value = stats[6] else print("No current stats exist. Creating new ones") val.Value = 0 val2.Value = 1 val3.Value = 0 val4.Value = 10 val5.Value = 500 val6.Value = false end end game.Players.PlayerRemoving:Connect(function(player) local stats = {} --put the references to the current leaderstats you want-- local success,errormessage = pcall(function() ds:SetAsync(player.UserId,stats) end) if success then print("Saved Stats") end end)