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

script not changing values and camera?

Asked by 6 years ago
Edited 6 years ago

when i do this script here when i press the button it should change the value and switch the camera but it doesn't

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
local h = script.Parent.Parent.Parent.House.Value
script.Parent.MouseButton1Click:Connect(function()
    if h == "House1" then
        camera.CameraSubject = workspace.H2
        h = "House2"
    end
    if h == "House2" then
        camera.CameraSubject = workspace.H3
        h = "House3"
    end
    if h == "House3" then
        camera.CameraSubject = workspace.H4
        h = "House4"
    end
    if h == "House4" then
        camera.CameraSubject = workspace.H1
        h = "House1"
    end
end)
0
the value is probably not changing greatneil80 2647 — 6y
0
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 Vulkarin 581 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
local h = script.Parent.Parent.Parent.House

script.Parent.Activated:Connect(function()
    if h.Value == "House1" then
        camera.CameraSubject = workspace.H2
        h.Value = "House2" -- Directly accessing the property. 
    elseif h.Value == "House2" then -- use elseif! instead of creating new ifs
        camera.CameraSubject = workspace.H3
        h.Value = "House3"
    elseif h.Value == "House3" then
        camera.CameraSubject = workspace.H4
        h.Value= "House4"
    else
        camera.CameraSubject = workspace.H1
        h.Value = "House1"
    end
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 numberVal = num

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.

Ad
Log in to vote
-2
Answered by 6 years ago

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.

1
You literally just copy-pasted incapaz’s answer... User#20279 0 — 6y
0
Not saying you should copy, but if you are copying an answer, at least make it look like you didn't directly copy it. hiimgoodpack 2009 — 6y

Answer this question