Ive been working on a little minigame that randomly displays things on an imagebutton, I can’t find any issues with my script yet for some reason only the first decal shows when its chosen
script :
local Value = script.Parent.Parent.Counter.Value
script.Parent.MouseButton1Click:Connect(function()
Value.Value = Value.Value + 1
script.Parent.Position = UDim2.new(math.clamp(math.random(), 0.2, 0.8), 0, math.clamp(math.random(), 0.2, 0.8), 0)
--local Vegetable = script.Parent.Image
local ImageButton = script.Parent
local Number = math.random(1, 3)
local Lettuce = "rbxassetid://12362066125"
local Tomato = "rbxassetid://12362101220"
local Bread = "rbxassetid://12362108286"
local function pickLettuce()
ImageButton.Image = Lettuce
end
local function pickTomato()
ImageButton.Image = Tomato
end
local function pickBread()
ImageButton.Image = Bread
end
if Number == 1 then
pickLettuce()
elseif Number == 2 then
pickTomato()
elseif Number == 3 then
pickBread()
end
print(ImageButton.Image)
end)
you can make your code more organized and easier to use by doing something like this :
local coolfoods = {
["Lettuce"] = "rbxassetid://12362066125";
["Tomato"] = "rbxassetid://12362101220";
["Bread"] = "rbxassetid://12362108286";
}
local function chooserandomfood()
local totalfood = #coolfoods --// getting how many objects are in the table
local mycoolrandomnumber = math.random(1,totalfood) --// generate random number
for index,foodimg in pairs(coolfoods) do --// loop trough table
if mycoolrandomnumber == index then --// check if current index is same as our random number
print(foodimg) --// print img id
break; --// break the for loop
end
end