How would I be able to remember to use .Value?

Hello former developers! I keep making mistakes in my scripts. I often keep changing everything and when I add a .value at the end, that fixes the script. Here is an example:


Broken script:

game.ReplicatedStorage.EndRace.OnClientEvent:Connect(function()
	if script.Parent.Parent.Parent.Parent.Bools.AllowedToEndRace.Value == true then
		script.Parent.Parent.Visible = true
		script.Parent.Text = "".. script.Parent.Parent.Parent.Parent.StopWatch.Window.Label.Text ..""
		if script.Parent.Parent.Parent.Parent.Parent.Times.SpaceHighway.Value >= script.Parent.Parent.Parent.Parent.Parent.Times.SpaceHighway then
			script.Parent.Parent.Parent.Parent.Parent.Times.SpaceHighway = script.Parent.Parent.Parent.Parent.StopWatch.Window.StopwatchValue
		end
	end
end)

Working script:

game.ReplicatedStorage.EndRace.OnClientEvent:Connect(function()
	if script.Parent.Parent.Parent.Parent.Bools.AllowedToEndRace.Value == true then
		script.Parent.Parent.Visible = true
		script.Parent.Text = "".. script.Parent.Parent.Parent.Parent.StopWatch.Window.Label.Text ..""
		if script.Parent.Parent.Parent.Parent.Parent.Times.SpaceHighway.Value >= script.Parent.Parent.Parent.Parent.Parent.Times.SpaceHighway.Value then
			script.Parent.Parent.Parent.Parent.Parent.Times.SpaceHighway.Value = script.Parent.Parent.Parent.Parent.StopWatch.Window.StopwatchValue.Value
		end
	end
end)

What do you think the problem is? Well it is that I forgot .value. It has been a bad habit of mine for a couple of years now. I now want to find out how to break out of this habit. If you know a way, please let me know. Thanks!

WEcompany

1 Like

How about making variables at the beginning of the script so it doesn’t have to read through all those items each time it runs?
example:

endrace = script.Parent.Parent.Parent.Parent.Bools.AllowedToEndRace
spacehighway = script.Parent.Parent.Parent.Parent.Parent.Times.SpaceHighway
stopwatch = script.Parent.Parent.Parent.Parent.StopWatch.Window

game.ReplicatedStorage.EndRace.OnClientEvent:Connect(function()
	if endrace.Value then
		script.Parent.Parent.Visible = true
		script.Parent.Text = "".. stopwatch.Label.Text ..""
		if spacehighway.Value >= spacehighway.Value then ((why? this Value will never be higher than itself, if you put "if spacehighway.Value then" it is going to check to see if the Value is not nil and keep running))
			spacehighway.Value = stopwatch.StopwatchValue.Value
		end
	end
end)

Then when you write the script you won’t be confused by all the extra stuff you put in there and will hopefully remember that a value item needs to have .Value put after it to get the number.

Also, if any pro scripters read this and find issues, I’m definitely not a scripter, but @WEcompany’s script screamed at me to be cleaned up.

1 Like

Being mindful, naming your variables properly and writing clean code will help out a lot, it’s impossible to forget / ignore something that is screaming information to your face.

1 Like

Hi,

It is important to understand the difference between a Value instance and the .Value property. A Value instance is an object in the game hierarchy that has a Value property. The Value property is the actual value it holds. Seems obvious, yes, but many people think these two things are the same.

That said, keep this in mind:

  • Whenever you want to use the value, use valueInstance.Value.
  • When you want to modify the Value instance itself (it’s an Instance, so it has a Name and a Parent), use valueInstance.

One more thing: Try to avoid excessive parent references (script.Parent.Parent.Parent) as it can be confusing and hard to read. Try and set up a good structure for your project, perhaps having these values somewhere in a top level folder, so you do not have to go through so many instances.

Hope this helps

I’ve moved your topic to #help-and-feedback:scripting-support since this doesn’t relate to a dev discussion. Please remember to use the correct categories, thanks.

2 Likes

What do you mean by “former developers”?

1 Like