Hello, I'm making a data store changer for a speedrun game. I already have the data store set up, and I'm working on the data store updater. Here is my code:
local Players = game:GetService("Players") local pausetime = script.Parent local time = script.Parent.Parent:FindFirstChild("SpeedunTimerGUI") local function onPauseButtonTouch(touchpart) local partParent = touchpart.Parent local humanoid = partParent:FindFirstChildWhichIsA("Humanoid") if humanoid then local player = Players:GetPlayerFromCharacter(partParent) local leaderstats = player.leaderstats local pb = leaderstats and leaderstats:FindFirstChild("Best Time") if pb then if leaderstats.BestTime > time then leaderstats.BestTime.Value = time end end end end pausetime.Touched:Connect(onPauseButtonTouch())
I get the error on line 7 telling me "Workspace.Speedrun Timer.Timer_Pause.Script:7: attempt to index nil with 'Parent'". What is the problem with referencing the parent?
The error Workspace.Speedrun Timer.Timer_Pause.Script:7: attempt to index nil with 'Parent'
basically means whatever you're trying to get the Parent of, is nil. In this case, that would be touchpart
.
Why is it nil? The issue is at line 20. When connecting the Touched
event to onPauseButtonTouch
function, you're actually calling the function instead of connecting it, by putting two brackets ()
behind it.
This caused your function to run at line 20, and since you haven't given the function any arguments, touchpart
was nil.
So instead of pausetime.Touched:Connect(onPauseButtonTouch())
, the appropriate code for this case is pausetime.Touched:Connect(onPauseButtonTouch)
.