You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I would really like to figure out why I get this error. It doesn’t happen often; 49 times out of 50, the code works just fine.
What is the issue? Include screenshots / videos if possible!
The code, which is contained within ReplicatedFirst:
58 local w = game:GetService("Workspace")
...
65 local tweens = game:GetService("TweenService")
...
70 local board = workspace:WaitForChild("baseboard")
...
82 local vec3 = Instance.new("Vector3Value")
83 vec3.Value = Vector3.new(-56.5,1.3,-56.6)
...
88 local TargetTween = tweens:Create(vec3, TweenInfo.new(10,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut), {Value=board.PrimaryPart.Position - Vector3.new(0,15,3)}) -- This is the line that throws for some reason...
The error, which only pops up every once in a while:
ReplicatedFirst.FirstScript:88: attempt to index nil with 'Position'
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried adding a task.wait() on line 86 to see if that would somehow give “PrimaryPart” more time to initialize or something; it didn’t, and I’m wondering if it’s just a fluke because I’m running in Studio.
I just realized I had something redundant in my code, you don’t have to point out that "I could have used w instead of workspace on line 70, that isn’t the problem anyway.
It simply means that the board.PrimaryPart has not been loaded yet when the TargetTween is created.
Make sure the PrimaryPart is loaded before you create the tween.
i assume it is a local script right? The model might have loaded but the primary part might NOT be in the model due to streaming enabled being turned on . What streaming enabled does is hide all the base parts that are far away from the player. Thus , i think parenting the model in replicate first will help???
I will try this, then. I’ve put the board in ReplicatedFirst so it should be initialized with the script. I’m not getting any errors so far, but like I said, it doesn’t throw every single time. It could still happen so I’m keeping my fingers crossed.
Add a task.wait(?) maybe and move local board to the bottom of that define area.
" It doesn’t happen often " … it it right just missing a define sometimes
assuming that this is a LocalScript, i’d find it likely that board.PrimaryPart is loading afterTargetTween is called. I’d yield the code and wait for board.PrimaryPart to be loaded before calling TargetTween. you can do this similarly to how you’re doing it already, by calling :WaitForChild() with the parameter set to board’s PrimaryPart
adding onto this, to explain the inconsistency with the error, i’d say it’s because of varying loading times for board.PrimaryPart. so sometimes it loads, sometimes it doesn’t
I guess that makes sense. Sometimes it takes longer to load than the time it takes to fetch it in the script. It shouldn’t be a problem now because it should now load before the script runs.