Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script does not change a value? (This isn't specific enough?)

Asked by 5 years ago

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

0
Does the studio testing show any errors? Knineteen19 307 — 5y
0
Oh wait sorry I didn't read your question... Knineteen19 307 — 5y
0
Probably because you need "Job" as a local. Stycon 30 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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!

0
You don't "call" variables. User#19524 175 — 5y
0
You know what I mean, I'm still learning scripting myself okay... Knineteen19 307 — 5y
0
incapaz did not say the same thing as you. He said a lot more. Your answer would never work. @incapaz 's answer will. Please refrain from answering questions if you are "not entirely sure" in the future. User#21908 42 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

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 & 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.

Value

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.

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
    -- 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.

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
        -- 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)
0
I basically just said the same thing but in less words, but thanks for the information I guess? Knineteen19 307 — 5y
0
You didn't explain truthy values and why the if statement would always execute. User#19524 175 — 5y
0
I was going to answer this question, but as I scrolled down I saw that you had already answered and I cried. User#21908 42 — 5y
0
Incredible job by the way. User#21908 42 — 5y

Answer this question