I am making a vending machine, and I want to make it so, when you click it, you are given a Bloxy Cola.
You’d probably want to start off with a ClickDetector that’s inside of the vending mechine. When the clicked event is fired, you can clone a BloxyCola and position it in front of the machine so that it can be picked up by the player. Here’s some more information about ClickDetectors that should help you: ClickDetector | Documentation - Roblox Creator Hub
Here’s an example I wrote of what you’ll probably want to base your code on:
local ClickDetector, BloxyCola, VendingMachine
ClickDetector.MouseClick:Connect(function() -- When someone clicks the ClickDetector, this function is called
local ToDispense = BloxyCola:Clone() -- Clone the tool
ToDispense.Parent = worksapce -- Parent it to the workspace so it's rendered in the physical world
ToDispense.PrimaryPart.CFrame = CFrame.new(VendingMachine.PrimaryPart.Position.X + 5, VendingMachine.PrimaryPart.Position.Y, VendingMachine.PrimaryPart.Position.Z) -- Position it in-front of the vending machine, this may not be in-front of it; just an example of how to position it
end)
BloxyCola.rbxl (26.1 KB)
I’m not a great scripter yet, but you could try:
put the cola in server storage or something
add a ClickDetector on the button you want them to click, and insert a script:
function give(plr)
local cola = game.ServerStorage:Clone() --clone the cola
cola.Parent = plr.Backpack --puts it into the player's backpack, but it might be plr.Character.Backpack, sorry I'm not the best at scripting
end
script.Parent.MouseClick:Connect (give)
Not 100% sure if this will work, but I’m pretty sure.
Alright, looking into it now. Should be working soon.
I am maybe a pretty late but why are you not using Model:SetPrimaryPartCFrame()?
Would be something like this:
local ClickDetector, BloxyCola, VendingMachine
ClickDetector.MouseClick:Connect(function() -- When someone clicks the ClickDetector, this function is called
local ToDispense = BloxyCola:Clone() -- Clone the tool
ToDispense.Parent = worksapce -- Parent it to the workspace so it's rendered in the physical world
ToDispense:SetPrimaryPartCFrame(CFrame.new(VendingMachine.PrimaryPart.Position.X + 5, VendingMachine.PrimaryPart.Position.Y, VendingMachine.PrimaryPart.Position.Z)) -- Position it in-front of the vending machine, this may not be in-front of it; just an example of how to position it
end)