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

How do I use an if statement to check if a part has been clicked?

How do I use an if statement to check if a part has been clicked on? I'm using an If statement because... I really don't know how to explain it you'll understand when you read the script

local chat = game.Workspace.Dummy1.Head.Dialog
local Menu = {"pizza","fries","hamburger"}

local Item = math.random(1,#Menu)  -- chooses random Item from the table

chat.InitialPrompt = (Menu[Item]) -- says random item

local PizzaButton = game.Workspace.PizzaButton -- a variable for the button
local FriesButton = game.Workspace.FriesButton -- a variable for the button
local BurgerButton = game.Workspace.BurgerButton -- a variable for the button

if chat.InitialPrompt == "pizza" then -- checks what have random item has been choosen
	print("Pizza has been choosen")
	if  -- Here I want to check if a button has been clicked
elseif chat.InitialPrompt == "fries" then -- checks what have random item has been choosen
	print("Fries has been chosen")
	if -- Here I want to check if a button has been clicked
elseif chat.InitialPrompt == "hamburger" then -- checks what have random item has been choosen
	print("Burger has been choosen")
	if -- Here I want to check if a button has been clicked
else
	print("Error")
end

I hope you understand I'm not really good at explaining.. Also it is NOT a gui

Edit: Ok I am trying to make something like pizza place where the customer orders something random and you have to press a button so you could complete the order I hope this makes it easier to understand :)

1 comment

0

What are you trying to detect a part getting clicked or a GUI button?

2 answers

Looking at your scrip i think its a click detector you are talking about. So I think we will have to alter your script a bit. Instead of using if statements, you can use functions!

so you want to insert a click detector in your pizza fries and burgers. then create a function for each of them!

heres a function for fries:

local clickDetector = workspace.fries.ClickDetector -- make sure the object is called fries
 
function Click()
	print("EAT DA FRIES")
end
 
clickDetector.MouseClick:connect(Click)

If your button is a GUI you should use MouseButton1Click:

script.parent = friesbutton -- rename your gui into friesbutton
function clickfries()
  print("EAT DA FRIES")
end

script.Parent.MouseButton1Click:Connect(clickfries)
-- DO NOT ADD A CLICK DETECTOR FOR THIS SCRIPT THE GUI IS ALREADY A BUTTON

Hi I think you should insert a ClickDetector in the part and then do this script

local ClickD = --Define Click Detector here

ClickD.MouseButton1Click:Connect(function()
	--whatever u want it to do
end)


btw, this code will run at the specific moment when the button is clicked

OR

if u wanna see if a part has been clicked before in the past, then use this script

local ClickD = --Define Click Detector

ClickD.MouseButton1Click:Connect(function(player)
	player.HasClicked.Value = true
end)

HasClicked is a BoolValue in StarterPack

This code basically stores the value that the player has clicked the part and the value can be used later

hope this helps

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