hours is not valid member of Folder "Players.trynotazies.leaderstats" - roblox

For some reason when I am trying to save the data for my leader stats script it says not a valid member. Any help would be appreciated right now. This is for a game I am making on Roblox and hoping for it to grow popular
Here is the code:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local hours = Instance.new("IntValue")
hours.Name = "Hours"
hours.Value = 0
hours.Parent = leaderstats
local Sleep_Tokens = Instance.new("IntValue")
Sleep_Tokens.Name = "Sleep Tokens"
Sleep_Tokens.Value = 0
Sleep_Tokens.Parent = leaderstats
local data
local data2
local success, errormessage = pcall(function()
data = DataStore:GetAsync(player.UserId.."-hours")
data2 = DataStore:GetAsync(player.UserId.."-sleeptokens")
end)
if success then
hours.Value = data
Sleep_Tokens.Value = data2
else
print("Had an error while retrieving player data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId.."-hours",player.leaderstats.hours.Value)
DataStore:SetAsync(player.UserId.."-sleeptokens",player.leaderstats.Sleep_Tokens.Value)
end)
if success then
print("Player Data Saved")
else
print("Had an error while saving player data")
warn(errormessage)
end
end)

The error is telling you that hours isn't a child of the player's leaderstats. This is because object names are case-sensitive, and when you created the IntValue, you named it Hours :
local hours = Instance.new("IntValue")
hours.Name = "Hours"
hours.Value = 0
hours.Parent = leaderstats
When you are saving the data, you either need to index the leaderstats using the proper name Hours or change the IntValue's Name to be hours. You'll run into the same issue with Sleep_Tokens.
Try this :
DataStore:SetAsync(player.UserId.."-hours", player.leaderstats.Hours.Value)
DataStore:SetAsync(player.UserId.."-sleeptokens", player.leaderstats["Sleep Tokens"].Value)

Related

Roblox not checking when bool value is updated?

So I've been trying to make a script on Roblox where tubes turn black when a Boolean is changed to false but it doesn't detect when it becomes false.
local Coolant = workspace.Temperature.CoolantActive
local CoolTube = workspace.TheCore.Coolant.Cool1
while true do
if Coolant.Value == true then
CoolTube.Material = ("Neon")
CoolTube.BrickColor = BrickColor.new("Toothpaste")
elseif Coolant.Value == false then
CoolTube.Material = ("Metal")
CoolTube.BrickColor = BrickColor.new("Really black")
end
wait(1)
end
I have no idea why this is happening, help?!
Rather than having a loop run forever, try using the Changed signal to detect when the Value changes.
local Coolant = workspace.Temperature.CoolantActive
local CoolTube = workspace.TheCore.Coolant.Cool1
Coolant.Changed:Connect(function(newValue)
if newValue == true then
CoolTube.Material = Enum.Material.Neon
CoolTube.BrickColor = BrickColor.new("Toothpaste")
else
CoolTube.Material = Enum.Material.Metal
CoolTube.BrickColor = BrickColor.new("Really black")
end
end)

Cannot set optional value, prints nil

I am working on project that writes to google sheets. I am trying to Unmerge cells. This function works however it unmerges everything in the sheet. This is because it is not setting the .range value. When I print (as seen below) the "test" value all range values are shown accordingly however when I print the "request.unmergeCells?.range" it says nil. I am further confused as I use this exact code elsewhere for a merge command and it loads the values fine (see second snippet of code.)
I have tried for days to resolve this issue with no avail. Any thoughts?
func unmergecell1() {
let request = GTLRSheets_Request.init()
let test = GTLRSheets_GridRange.init()
rowstart = 4
rowend = 100
columnstart = 0
columnend = 100
test.startRowIndex = rowstart
test.endRowIndex = rowend
test.startColumnIndex = columnstart
test.endColumnIndex = columnend
request.unmergeCells?.range = test
request.unmergeCells = GTLRSheets_UnmergeCellsRequest.init()
print("=========unmerge==============")
print(test)
print(request.unmergeCells?.range)
let batchUpdate = GTLRSheets_BatchUpdateSpreadsheetRequest.init()
batchUpdate.requests = [request]
let createQuery = GTLRSheetsQuery_SpreadsheetsBatchUpdate.query(withObject: batchUpdate, spreadsheetId: spreadsheetId)
service.executeQuery(createQuery) { (ticket, result, NSError) in
}
}
func mergecell() {
let request = GTLRSheets_Request.init()
request.mergeCells = GTLRSheets_MergeCellsRequest.init()
let test = GTLRSheets_GridRange.init()
test.startRowIndex = rowstart
test.endRowIndex = rowend
test.startColumnIndex = columnstart
test.endColumnIndex = columnend
request.mergeCells?.range = test
request.mergeCells?.mergeType = kGTLRSheets_MergeCellsRequest_MergeType_MergeRows
let batchUpdate = GTLRSheets_BatchUpdateSpreadsheetRequest.init()
batchUpdate.requests = [request]
let createQuery = GTLRSheetsQuery_SpreadsheetsBatchUpdate.query(withObject: batchUpdate, spreadsheetId: spreadsheetId)
service.executeQuery(createQuery) { (ticket, result, NSError) in
}
}
I think you need to switch these 2 lines:
request.unmergeCells?.range = test
request.unmergeCells = GTLRSheets_UnmergeCellsRequest.init()
The 2nd line initates the unmerge request, and only after you initate it can you add the range to it. So by making it the other way round, as below, it should work.
request.unmergeCells = GTLRSheets_UnmergeCellsRequest.init()
request.unmergeCells?.range = test

Getting Blood Glucose from HealthKit Query

I am trying to pull blood glucose values from a query I have made to get the last 10 in the Health App. I am able to pull the data from the Health app and get it in the form for a single entry:
(2017-02-21 13:13:00 -0500 - 2017-02-21 13:13:00 -0500), 80 mg/dL 52D7A973-7853-455C-9308-0E339153A3BE "Health" (10.2.1) metadata: {
HKWasUserEntered = 1;}
I am able to pull the startdate (and endDate) from this by adding:
guard let timing = reading?.endDate as Date? else {
print("timing scheme didn't work.")
return
}
print(timing)
and pull the type (HKQuantityTypeIdentifierBloodGlucose) by writing
guard let bg = reading?.sampleType as HKSampleType? else {
print("bg scheme didn't work")
return
}
print(bg)
However, I can not figure out to print the actual blood glucose value (ie. 80 mg/dL).
I tried:
let bg = reading.quantity
but I got the error
Value of type 'Optional<HKSample>' has no member 'quantity'
If anyone has insight to this, it would be extremely helpful! Thank you in advance!
// To read blood glucose:
let gluco = reading as? HKQuantitySample
if let bg = gluco?.quantity {
print(bg)
}

NSUserDefafults returns wrong data

On my Cocoa application using Swift, I'm getting bad data from NSUserDefaults. If I go to the command line and do
defaults read my.bundle.identifier
It comes back with this:
{
NSNavLastRootDirectory = "~/Documents";
NSNavPanelExpandedSizeForSaveMode = "{712, 521}";
producer = 10;
version = "-1";
when = 1470502459;
}
Then, in my application I read the value of version like so:
let def = NSUserDefaults.standardUserDefaults()
let current = def.integerForKey("version")
print("Current version is \(current)")
It's printing out that current is 3, not -1. What in the world is going on?

Error accessing recordFields in CKQueryNotification

When I access the recordFields on a CKQueryNotification returned from a subscription, I get this error:
Could not cast value of type '__NSCFDictionary' (0x1009f0630) to 'CKRecordValue' (0x1009fc050).
I process the notification like this:
let notification: CKNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
It seems to work fine and I get a CKQueryNotification.
The notification recordFields prints out like:
<CKQueryNotification: 0x156ef2bf0; notificationType=1, notificationID=<CKNotificationID: 0x156ef41e0; UUID=ba82325d-ab9e-4805-bb26-994a1122900b>, containerIdentifier=iCloud.com.skyvalleystudio.RaceTiming, subscriptionID=Marks for 06597454-4EAD-434E-BC03-CE51D987C79F, queryNotificationReason=1, recordFields={
location = {
altitude = 0;
course = "-1";
horizontalAccuracy = 0;
latitude = "37.33296424";
longitude = "-122.03937369";
speed = "-1";
timestamp = "484712160.263403";
verticalAccuracy = "-1";
};
timeStamp = 484712160;
user = "_2031e89e9ade8345c7d4248f3ec3a2e6";
}, recordID=<CKRecordID: 0x156ef1670; 568D257C-E849-4492-A810-CB7076FC5A22:(_defaultZone:__defaultOwner__)>, isPublicDatabase=1>
So it looks like the recordFields dictionary is there and populated.
I try to access is like this:
let timestamp = notification.recordFields!["timeStamp"] as? NSDate
Then I get the error.
UPDATE
I filed a Apple bug report 26251611.
The CLLocation is not being decoded in the CKNotification creation is my best guess.
One possible workaround is processing the userInfo directly, but I had problems with processing the CKReference. It seems like the CKRecordValue protocol adopters should be better handled when the CKNotification is created.
UPDATE
Closed as duplicate of 24643471. It's still open at this time...