TextButton not executing a function

Hey,
I have recently been promoted to a new member and have been eager to post about my problem.

I’m currently trying to create a vending machine from an old roblox model.
image
I have modified it a lot and gave it a GUI upon pressing the pistol button. (too lazy to change decal)
The GUI shows up fine after being pressed, but the TextButton (Bloxy Cola) does not work.
image

local drink = game.ReplicatedStorage.BloxyCola
local spot = script.Parent.Parent.Parent.Parent.Parent.Holder
local db = false



	function parentPressed()
		if db == false then
		db = true
		local d = drink:clone()
		d.Handle.Position = spot.Position
		d.Parent = game.Workspace
		print("Vending Machine is dispensing")
		wait(5)
		db = false
		end
	end		
	
script.Parent.MouseButton1Down:connect(parentPressed)

The soda is supposed to be cloned on the Holder part.
image

Any help would be greatly appreciated, I’m not sure if I sent enough information though.

1 Like

If the code is in the Script, it will not execute, as Scripts (aka ServerScripts) only execute on the Server, which means it cannot know if a certain user/client provides input on an interface.

If the code is in the LocalScript, it will also not execute, as LocalScripts only execute in Workspace if they are a descendant of a user’s Character.

The fix here is to make sure the code is inside a LocalScript, move that LocalScript to a place in which it will execute, and modify the code to compensate for its new location.

EDIT: Scratch that; just noticed that it inserts the GUI into the Player’s PlayerGui and is not a SurfaceGui. Notice this line:

local spot = script.Parent.Parent.Parent.Parent.Parent.Holder

This line will execute (assuming it is in a LocalScript) and not be able to find Holder, as Holder is in the vending machine’s model, while the LocalScript is now under PlayerGui. A good solution to this problem would be to have an ObjectValue within the VendingMachine object with its Value set to Holder as the GUI is inserted into the player’s PlayerGui.

4 Likes

Thanks a lot for responding!
The function has now executed, but the soda still did not get cloned and now there’s an error in the console.
image
Any idea how to fix that?

Sounds like you’re accidentally doing ObjectValue.Position instead of ObjectValue.Value.Position.

It is really important to learn how to diagnose/debug errors, it’s easy and very self explainable. Postition is not a valid member of ObjectValue, means that whatever object you did .Position on doesn’t have that property. In your case it’s the spot object thta you’re defining in the variable, which means that the hierachy/pathway to that object is wrong, are you sure that it’s correct?

2 Likes

Fixed it, but now the tool is not being cloned on the desired position, nor is it equipable.
Could this be because it’s in ReplicatedStorage or because of something else?

Hard to say without more information. Could be that you are setting the Position property instead of the CFrame property (setting Position isn’t always predictable); could be that Handle has CanCollide = false. If it is unequippable, that would very likely be unrelated to the original problem.

Fixed the location problem, I moved the soda to the Workspace and it cloned at the right position.
Regarding the equipping, yes you may be right. The problem could be in the cola model itself, I’ll try fixing it.

Also, thanks a lot for your help or else I still would be ragequitting studio and just go back at playing games.

1 Like