Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

(Noob here)(Simple Script) Need help with a Vector3 Function?

Asked by 3 days ago
Edited 3 days ago

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

2 answers

Log in to vote
1
Answered by
Fifkee 2016 Administrator Moderation Voter Community Moderator
3 days ago

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])

0
Oh Alright thanks. Im kind of new to scripting so I wanted to expirement with some stuff. Im kinda dumb and didnt realize the function needs to be a parameter same with math.random DVSRobert 7 — 2d
Ad
Log in to vote
1
Answered by 3 days ago

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

https://developer.roblox.com/en-us/articles/Debounce

0
It's not mandatory for Vector3s to have all three values. As a matter of fact, you could do Vector3.new() and it'd work just as fine. Also, math.random doesn't require arguments. And, lastly, please don't say "x" is incorrect, tell them why it's incorrect. Good answer tho :0 Fifkee 2016 — 3d
0
thank you for the feedback. about the math.random thing it may not require arguments but in this context its better to use it with arguments. as adding decimal values to an object in workspace wouldnt really change its position at all CaioAlpaca 348 — 3d

Answer this question