Hey guys my name is DVSRobert and I was wondering what was wrong with my script. Im trying to make a script where when a Part is touched it moves to a Diffrent position the code is saying that I need to close my function at line 5 even though Ive already ended it at line 5. What Did i do wrong?
local function DoDOMove() script.Parent.Position = Workspace.DoDO.Position + Vector3.new(math.random)`` end script.Parent.Touched:Connect(DoDOMove() --BTW if you couldnt tell DoDO is a part
script.Parent.Touched:Connect(DoDOMove()
should be script.Parent.Touched:Connect(DoDOMove)
. The Connect function takes in a function that is called whenever the signal is fired, however you're providing a function call. And you're missing parenthesis.
Lowercase Workspace
. It's deprecated.
I'd also advise removing the backticks from the end of line 3.
Also, are you aware that math.random is a function, not an object / property? Call it by doing math.random()
. If you want it to return an integer from x
to y
, then do math.random(x,y)
; x and y being the min and max value, since math.random()
by itself returns a non-integer value (range [0, 1])
So there are some issues on your code, ill list them
.1 Line 6 is incorrect, it has to be like this
script.Parent.Touched:Connect(DoDOMove)
-
.2 math.random doesnt work like that, you have to give it a min and a max number like this:
print(math.random(1, 5)) --will print a pseudorandom number from 1 to 5
-
.3 Theres no reason to do this in your case
script.Parent.Position = Workspace.DoDO.Position +
You should just do this
script.Parent.Position = yourValues --this is just an example.
-
.4 Vector3 has three axes, X Y and Z. You are setting it to math.random so it wont work.
You have to create a random number for each axis, so you could do something like this
script.Parent.Position = Vector3.new(math.random(1, 5), math.random(2, 5), math.random(5, 10)) --[[ EXPLANATION X = math.random(1, 5) == pseudorandom number between 1 and 5 Y = math.random(2, 5) == pseudorandom number between 2 and 5 Z = math.random(5, 10) == pseudorandom number between 5 and 10 ]]
-
.5 Not an error just an observation, you should probably add a debounce to that script