Can anyone help with ClickDetector events?

local dice2 = game.Workspace.Dice2

local button = game.Workspace.Button
local clickdetector = button:FindFirstChild("ClickDetector")

local d1_pos_rand_vector3_X = math.random(20, 24)
local d1_pos_rand_vector3_Y = math.random(20, 24)
local d1_pos_rand_vector3_Z = math.random(-5, -3)

local d1_rot_rand_vector3_X = math.random(-180, 180)
local d1_rot_rand_vector3_Y = math.random(-180, 180)
local d1_rot_rand_vector3_Z = math.random(-180, 180)

local d2_pos_rand_vector3_X = math.random(20, 24)
local d2_pos_rand_vector3_Y = math.random(20, 24)
local d2_pos_rand_vector3_Z = math.random(3, 5)

local d2_rot_rand_vector3_X = math.random(-180, 180)
local d2_rot_rand_vector3_Y = math.random(-180, 180)
local d2_rot_rand_vector3_Z = math.random(-180, 180)

button.ClickDetector.MouseClick:Connect(function()
    dice1.Position = Vector3.new(d1_pos_rand_vector3_X, d1_pos_rand_vector3_Y, 
    d1_pos_rand_vector3_Z)
    dice1.Orientation = Vector3.new(d1_rot_rand_vector3_X, d1_rot_rand_vector3_Y, 
    d1_rot_rand_vector3_Z)
    dice2.Position = Vector3.new(d2_pos_rand_vector3_X, d2_pos_rand_vector3_Y, 
    d2_pos_rand_vector3_Z)
    dice2.Orientation = Vector3.new(d2_rot_rand_vector3_X, d2_rot_rand_vector3_Y, 
    d2_rot_rand_vector3_Z)
    wait(8)
    dice1.Anchored = false
    dice2.Anchored = false
end

Does anyone know why this isn’t working? I am trying to get these two dice to roll randomly at the click of a part.

2 Likes

are the variables offputting? i can change them so it is easier to read

local clickDetector = workspace.Part.ClickDetector
function onMouseClick()
print("hi world") -- code goes here
end
clickDetector.MouseClick:connect(onMouseClick)
1 Like

AH YES, my error was I did:

clickdetector.MouseClicked:Connect(buttonClicked)

instead of:

clickdetector.MouseClick:Connect(buttonClicked)

I think your script should look like this

local clickDetector = game.workspace.Part.ClickDetector
local function onMouseClick()
print("hi world")
end
clickDetector.MouseClick:Connect(onMouseClick)

If you’re still getting the error, couldn’t you just put all of your Dice Properties inside tables? :thinking: I’d also use a BodyAngularVelocity & BodyVelocity to rotate the dice more easily that way, cause all you’re really just doing is setting the “Orientation” of the Dice once and that’s it

1 Like

I appreciate the feedback but I’m not familiar to bodyangularvelocity or bodyvelocity. I don’t know what tables are either, but I’ll be sure to look them up on the DevHub!

Edit: I found the solution, I was typing the MouseClick function as MouseClicked

Ah gotcha, do make sure to mark the post that helped you as a solution then!

Also those objects can pretty much be used in relation to physics, for your situation though if you want: BodyVelocity could hold up the Dice easily in the air without setting the Anchored property to true, & BodyAngularVelocity can be used to spin the Dice :wink:

1 Like

Ohh, right, thank you. I aren’t going to mark the post that helped me as the solution because I realised my mistake through it rather than them giving me a direct answer

You should still at least give a solution if you’ve already fixed the issue, so that other members of the community are aware of it being fixed

1 Like

Great point. I’ll post it in a reply to your comment:

Turns out I made a simple error when it came to writing out the MouseClick event. I wrote MouseClicked instead, oops!

As well as this, make sure you put the math.random() variables inside the anonymous function of the MouseClick event, otherwise you will not get a random die roll.

1 Like