This script does not change the StringValue I have in my game. I don't see anything wrong with it and there are no output errors in the script. Job equals Belfast ME to Augusta ME, I've made sure of that, and still nothing.
Job = game.Players.LocalPlayer.Job.Value if Job == "Belfast ME to Augusta ME" or "Belfast ME to Farmington ME" then script.Parent.Value = "Turn left onto Maine State Route 3" end
NOTE I do not live here, I am just planning to build an American Truck Simulator like game, which is why real life cities are involved
I'm not entirely sure, but maybe it's because you made the job variable equal to the value of the job. This just takes the variable, and not the actual value when you call the variable. So basically change the variable to game.Players.LocalPlayer.Job and add .Value to the end of each time you call the Job variable. So,
Job = game.Players.LocalPlayer.Job if Job.Value == "Belfast ME to Augusta ME" or "Belfast ME to Farmington ME" then script.Parent.Value = "Turn left onto Maine State Route 3" end
Hope I helped!
The problem lies in the fact that you wrongly assume that the Job
variable is a reference to the actual property of the StringValue
. If the value of the StringValue
changes, the variables you have defined won't update; only its Value
property will change. In order to fix this, you can remove the Job
variable and only make direct accesses to the Value
property of the StringValue
. You could also modify the variables to hold the StringValue
object itself.
local Job = game.Players.LocalPlayer.Job -- Use local variables. if Job.Value == "Belfast ME to Augusta ME" or "Belfast ME to Farmington ME" then script.Parent.Value = "Turn left onto Maine State Route 3" end
It is important to note the difference between a reference and a value. You can think of a reference as holding an actual memory location whilst a value just holds normal information (for example, a boolean).
In Lua, for example, dictionaries are passed via reference.
local t1 = { foo = "abc", bar = 123, foobar = true } local t2 = { foo = "abc", bar = 123, foobar = true } local t1reference = t1 --t1 & t1reference are the same dictionary now, but t2 is different t1.foo = "Hello world!" print(t1.foo) --> Hello world! print(t1reference.foo) --> Hello world! print(t2.foo) --> abc
The t1
and t1reference
variables both hold the same exact dictionary, whilst t2
is a different dictionary despite it holds the same values.
However, data like booleans and numbers are passed via value.
local number = 50 local numberValue = number number = 25 print(number) --> 25 print(numberValue) --> 50 local bool1 = false local bool1Val = bool1 bool1 = true print(bool1) --> true print(bool1Val) --> false
Even though numberValue
was set to number
, both variables have different memory and so changing one value does not change the other value. Same for the boolean values, bool1Val
never changed even after updating bool1
.
I'm not done.
In Lua, anything but false
and nil
is "truthy". So strings, numbers and tables are all examples of truthy values.
On line 2, you say
if Job == "Belfast ME to Augusta ME" or "Belfast ME to Farmington ME" then
That's almost the equivalent to if Job == "Belfast ME to Augusta ME" or true then
. Remember, truthy values.
if 582 then -- Executes because numbers are truthy values end if "Hello" then -- Executes because strings are truthy values end if {} then -- Executes because tables are truthy values end if nil then -- Does not execute because nil is a falsey value end
Your line always runs, because if the first condition is false, it will go to the second condition, and executes because a string is truthy. Even if the Value
was "HELLO HOW RU" it would still execute.
To fix your problem, you simply check if the Value
itself equals Belfast ME to Farmington ME
.
local Job = game.Players.LocalPlayer.Job Job.Changed:Connect(function(job) if job == "Belfast ME to Augusta ME" or job == "Belfast ME to Farmington ME" then -- Do something... end --[[ Use a changed event. Your if statement only executes once and it's instant. It will not execute again if the Job updates. The ValueBase.Changed event passes the new value as a parameter, so we can check it. ]]-- end)