The problem lies in the fact that you wrongly assume that the h 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 h 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.
view source
01
local camera = workspace.CurrentCamera
02
camera.CameraType = Enum.CameraType.Scriptable
03
local h = script.Parent.Parent.Parent.House
04
05
script.Parent.Activated:Connect(function()
06
if h.Value == "House1" then
07
camera.CameraSubject = workspace.H2
08
h.Value = "House2" -- Directly accessing the property.
09
elseif h.Value == "House2" then -- use elseif! instead of creating new ifs
10
camera.CameraSubject = workspace.H3
11
h.Value = "House3"
12
elseif h.Value == "House3" then
13
camera.CameraSubject = workspace.H4
14
h.Value= "House4"
15
else
16
camera.CameraSubject = workspace.H1
17
h.Value = "House1"
18
end
19
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.
view source
01
local t1 = {
02
foo = "abc",
03
bar = 123,
04
foobar = true
05
}
06
07
local t2 = {
08
foo = "abc",
09
bar = 123,
10
foobar = true
11
}
12
13
local t1reference = t1
14
--t1 & t1reference are the same dictionary now, but t2 is different
15
16
t1.foo = "Hello world!"
17
print(t1.foo) --> Hello world!
18
print(t1reference.foo) --> Hello world!
19
20
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.
view source
01
local number = 50
02
local numberVal = num
03
04
number = 25
05
print(number) --> 25
06
print(numberValue) --> 50
07
08
local bool1 = false
09
local bool1Val = bool1
10
bool1 = true
11
print(bool1) --> true
12
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.
2 comments
the value is probably not changing
there is such a thing as elseif, in other news if the Value changes at all then your script will not update...meaning you should probably define h within the MouseButton1Click event itself so you are certain that h is updated