Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I cant seem to get this script upp and running, whats wrong with it? :o

So I know the basics about scripting... But I cant seem to find the flaw in this one, since I tried a diffrent method.

print 'Hello world!'

local Button1 = script.Parent.Parent.Button1

local Gate = script.Parent.Parent.Gate1

local Value1 = script.Parent.Parent.Gate1.Value1.Value

local Hover = script.Parent.Parent.Gate1.Hover

function onTouched(p)
	if Value1 == 0
		then Hover.postion.Y = 255.076
	end
	if Value1 == 1
		then Hover.position.Y = 215.076
	end
end

script.Parent.Touched:connect(onTouched)

2 answers

There are just two issues with it, really.

Firstly, you can't set a variable to the property of another instance and use it in the way you have. When you do set a variable to the value of a property, the variable will 'become' whatever the property's value is at the point in time, not 'point to it' as people assume.

Secondly, you cannot set the X, Y and Z of the position in that manner.

(Oh, and you spelt 'position' wrong on line thirteen)

Here's an example of the 'fix' (however there's more than one way to do this, and I thought this would be the easiest to show you)

local Button1 = script.Parent.Parent.Button1
local Gate = script.Parent.Parent.Gate1
local Value1 = script.Parent.Parent.Gate1.Value1
local Hover = script.Parent.Parent.Gate1.Hover

function onTouched(p)
    if Value1.Value == 0 then
        Hover.position = Hover.position + Vector3.new(0, 40, 0)
    elseif Value1.Value == 1 then
        Hover.position = Hover.position - Vector3.new(0, 40, 0) 
    end
end

script.Parent.Touched:connect(onTouched)

On line 7, you should set the variable to the instance, not the value. This is because the variable will equal whatever the value is at the time it was created, and will not change as the value changes.

The XYZ coordinates of Vector3 are read only, which means you can't set them. You must do

Hover.position = Hover.position + Vector3.new(0, 255.076, 0)

This discussion is preserved as it was. New answers, comments, and votes are closed.