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
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
#Reference vs Value
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).
##Reference
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.foo = "Hello world!"
print(t1.foo)
print(t1reference.foo)
print(t2.foo)
The t1 and t1reference variables both hold the same exact dictionary, whilst t2 is a different dictionary despite it holds the same values.
##Value
However, data like booleans and numbers are passed via value.
local number = 50
local numberValue = number
number = 25
print(number)
print(numberValue)
local bool1 = false
local bool1Val = bool1
bool1 = true
print(bool1)
print(bool1Val)
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.
#Truthy values
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
end
if "Hello" then
end
if {} then
end
if nil then
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.
##Final product
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
end
end)
3 comments
Does the studio testing show any errors?
Oh wait sorry I didn't read your question...
Probably because you need "Job" as a local.