I need help with vector X Y and Z values and attachment properties

What’s wrong with this? I got a dice which rolls to produce a random result, and I have an attachment on each side of the dice. The script is supposed to detect whether the Y part of the WorldPosition on an attachment is 0 (meaning it is facing down on the baseplate)

local dice = script.Parent

local button = script.Parent.Parent:FindFirstChild("Button")
local clickdetector = button:FindFirstChild("ClickDetector")

local detector1 = script.Parent:FindFirstChild("Detector1")
local detector2 = script.Parent:FindFirstChild("Detector2")
local detector3 = script.Parent:FindFirstChild("Detector3")
local detector4 = script.Parent:FindFirstChild("Detector4")
local detector5 = script.Parent:FindFirstChild("Detector5")
local detector6 = script.Parent:FindFirstChild("Detector6")

local surfacegui = script.Parent.Parent.ResultDisplay:FindFirstChild("SurfaceGui")
local dice1result = surfacegui:FindFirstChild("Dice1Result")

local function buttonClicked()
	local positionx = math.random(20, 24)
	local positiony = math.random(20, 24)
	local positionz = math.random(-5, -3)

	local rotationx = math.random(-180, 180)
	local rotationy = math.random(-180, 180)
	local rotationz = math.random(-180, 180)
	
	dice.Position = Vector3.new(positionx, positiony, positionz)
	dice.Orientation = Vector3.new(rotationx, rotationy, rotationz)
	dice.Anchored = true
	wait(2)
	dice.Anchored = false
	wait(6)
	if detector1.WorldPosition.Y == 0 then
		dice1result.Text = "1"
	end
	if detector2.WorldPosition.Y == 0 then
		dice1result.Text = "2"
	end
	if detector3.WorldPosition.Y == 0 then
		dice1result.Text = "3"
	end
	if detector4.WorldPosition.Y == 0 then
		dice1result.Text = "4"
	end
	if detector5.WorldPosition.Y == 0 then
		dice1result.Text = "5"
	end
	if detector6.WorldPosition.Y == 0 then
		dice1result.Text = "6"
	end
end

clickdetector.MouseClick:Connect(buttonClicked)

I know its incredibly long, so I’ll be very appreciative for anyone who is willing to try to help me :slight_smile:

Have you checked to see the value of the WorldPositionY of the Attachment is actually 0 after the dice roll. If it’s even the slightest amount off 0 (like 0.00000001) then your if statements will all come back false.
You may want to try math.round for the Y value, then compare it to 0.

1 Like

great idea. ill try it and let you know if it works

do you mind giving me an example as to how math.round is used? i am not sure

Here is a visual representation. Notice the attachments on the dice (detectors in the script) I am trying to validate whether the WorldPosition of one is 0 (on the baseplate)

An example of how math.round works is how simple rounding works

print(math.round(0.4)) -- Prints 0
print(math.round(0.4999)) -- prints 0 I believe
print(math.round(0.6)) -- prints 1

You could round down your Y worldPosition in the if statement because it may not exactly be 0, example

if math.round(detector1.WorldPosition.Y) == 0 then
2 Likes

Thank you, I am testing it now… HOLY ! IT WORKED! thank you so much

this example was great but i will mark scottifly’s comment as the solution because he suggested it first

1 Like

Anytime! Glad to be of help to you once again! If you have anymore issues don’t be afraid to make another post!

And that’s completely fine by me, he mentioned it first so he does deserve it more than me, I jsut added more to his statement since you needed examples

1 Like

One more thing (sorry for dragging this out), but would you say that my method with the attachments is the most effective method of doing this, or is there a better way?

It can surely be changed around to make it a bit more simpler, especially since you’re repeating 6 times. I could help out, but I need ot know how the dice looks like i nthe Explorer

1 Like

Of course, here is an image thank you for your troubles

Something like this would be simpler

local dice = script.Parent

local button = script.Parent.Parent:FindFirstChild("Button")
local clickdetector = button:FindFirstChild("ClickDetector")

local attachments = {}

for _,v in pairs(dice:GetChildren()) do
	if not v:IsA("Attachment") then continue end
	table.insert(attachments,v)
end

local surfacegui = script.Parent.Parent.ResultDisplay:FindFirstChild("SurfaceGui")
local dice1result = surfacegui:FindFirstChild("Dice1Result")

local function buttonClicked()
	local positionx = math.random(20, 24)
	local positiony = math.random(20, 24)
	local positionz = math.random(-5, -3)

	local rotationx = math.random(-180, 180)
	local rotationy = math.random(-180, 180)
	local rotationz = math.random(-180, 180)
	
	dice.Position = Vector3.new(positionx, positiony, positionz)
	dice.Orientation = Vector3.new(rotationx, rotationy, rotationz)
	dice.Anchored = true
	wait(2)
	dice.Anchored = false
	wait(6)
	for _,attach in pairs(attachments) do
		if math.round(attach.WorldPosition.Y) == 0 then
			local name = attach.Name
			dice1result.Text = name:sub(#name)
			break
		end
	end
end

clickdetector.MouseClick:Connect(buttonClicked)
1 Like

Hmm, certainly looks alot compressed. I am not too sure of some of the things used in the script but I’ll be sure to look up more math functions and Vector3 features

It’s a simpler way of how you did it as it goes through the dice and adds all the Attachments in it in a table, so when it’s time to check, it goe sthrough the table and checks each of the attachments’ rounded Y WorldPosition if it’s 0 or not. And if it is, get the name of the attachment and get the last letter of the attachment, so you get the number to display, adn then break the loop

I get the concept. Good thinking!