Textbutton not working

Hey! For some reason my custom E to interact problem isnt working. Nothing prints out when I do it and I have double checked everything! It is suppossed to go to your invetory when you click E. Thanks for your time.

local UIS = game:GetService("UserInputService")

local Adornee = script.Parent.Parent.Cylinder
local tool = script.Parent.Parent.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		local magnitude = (HumanoidRootPart.Position - Adornee.Position).Magnitude
		print(magnitude)
		if magnitude <= 25 then
			tool.Parent = player.Backpack
		end
	end
end)

Interact local script

local tool = script.Parent.Parent.Parent.Parent

script.Parent.Activated:Connect(function()
	tool.Parent = game.Players.LocalPlayer.Backpack
end)

Textbutton local script

4 Likes

hello!
would you mind telling me where your interact script is parented?
if its not a descendant of the player than it may not run.

2 Likes

Sure! Here it is.
Screenshot 2024-01-19 172509

1 Like

ah!
your tool is parented to the workspace, unfortunately not a descendant of the player.

you should probably use server scripts (which will ALSO handle client replication) to actually give the players the tools, and local scripts just to detect input.

you could use remote events to do this.

add a remote event called “PickUpVitaminEvent” and:

for simplicity sake, you can merge your TextButton and Interact script, so change your Interact Script to:

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local tool = workspace:WaitForChild("Vitamins") --Change to wherever your vitamins are parented, could definitely use some optimization incase you have multiple vitamin tools
local Adornee = tool:WaitForChild("VitaminBottle"):WaitForChild("Cylinder") --Change to whatever your Adornee is
local Gui = tool.VitaminBottle:WaitForChild("VitaminBottle"):WaitForChild("BillboardGui"):WaitForChild("TextButton")

local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local PickUpEvent = game.ReplicatedStorage:WaitForChild("PickUpVitaminEvent") --Event that will fire to alert the server to pick up.

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		PickUpEvent:FireServer(Tool, Adornee) --Request to pick up item.
	end
end)

Gui.Activated:Connect(function()
    PickUpEvent:FireServer(Tool, Adornee) --Request to pick up item.
end)

and parent it to StarterPlayerScripts,
create a new script to handle the remote events and put in:

local PickUpEvent = game.ReplicatedStorage.PickUpVitaminEvent

PickUpVitaminEvent.OnServerEvent:Connect(function(playerWhoActivated, tool, adornee)
    if playerWhoActivated.Character then --Make sure player has a character (isn't dead/respawning)
        local magnitude = (playerWhoActivated.Character:WaitForChild("HumanoidRootPart").Position - adornee.Position).Magnitude
        print(magnitude)
        if magnitude <= 25 then
            tool:Clone().Parent = player.Backpack
        end
    end
end)

and put it in ServerScriptService.

i think thats it then, im pretty tired rn so the scripts aren’t perfect.
they would also need to be modified if there are going to be multiple tools instead of just 1.

if you encounter any issues or want me to explain anything, just respond!

1 Like

I got this in the local script! I usually am not good at figuring out what goes into client and what goes into server lol but heres the error i got!
Infinite yield possible on 'Workspace.Vitamins.VitaminBottle:WaitForChild(“VitaminBottle”)

Its not when you hit E its when everything successfully loads.

Guys please help! I dont know what the problem is!

i suggest using FindFirstChild instead of WaitForChild since that doesn’t guarantee it exists in workspace

also if the localscript is inside a tool, just directly use this one instead, waiting it from workspace is unnecessary and just causes a warning

local tool = workspace:WaitForChild("Vitamins")

to this

local tool = script.Parent --wherever the tool is according to script

It seems like you’ve shared three different scripts related to an interaction system, but it’s not clear what specific issue or bug you’re encountering. Could you provide more details about the problem or describe the behavior that is not working as expected?

In the meantime, here are a few general suggestions:

  1. Debugging Output: Add print statements to your code to output relevant information during script execution. This can help you identify where the issue might be occurring.
  2. Check for Errors: Review the Output panel in Roblox Studio for any error messages. If there are error messages, they can provide valuable information about what might be going wrong.
  3. Verify Parenting: Ensure that the parent-child relationships between objects are set up correctly. For example, check if the Adornee object is a child of script.Parent.Parent.Cylinder and if the tool is correctly parented.
  4. Input Service Check: Confirm that the UIS.InputBegan event is being triggered as expected. You can add a print statement inside the event handler to check if it’s responding to the E key press.
UIS.InputBegan:Connect(function(Input)
    print("Input began:", Input.UserInputType)
    if Input.KeyCode == Enum.KeyCode.E then
        -- Rest of your code
    end
end)

Check for Interference:
Ensure that there are no other scripts or components in your game that might interfere with the functionality of these scripts. Conflicting scripts could lead to unexpected behavior.

Please provide more details about the issue, any error messages you’re encountering, or the specific behavior that is not working as expected. This will help me provide more targeted assistance.

The inputbegan doesnt print because of the infinite yield, I have tried everything but nothing is working. These scripts are located in starterplayerscripts

  1. Check if Tool Exists: Before attempting to clone the tool to the player’s backpack, it’s a good idea to check if the tool actually exists to avoid potential errors.
  2. Consistent Variable Naming: Ensure consistency in variable naming. In your remote event script, you have a variable named PickUpEvent, while in the local script, you use PickUpVitaminEvent. I’ve adjusted it for consistency.
  3. Optimize Tool and Adornee Retrieval: Instead of using WaitForChild multiple times, you can directly access the tool and adornee if they are known.

Here’s the modified script:

-- Local Script (StarterPlayerScripts)
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local tool = workspace:WaitForChild("Vitamins") -- Change to wherever your vitamins are parented
local adornee = tool.VitaminBottle.Cylinder -- Change to whatever your Adornee is
local gui = tool.VitaminBottle.BillboardGui.TextButton

local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local PickUpEvent = game.ReplicatedStorage:WaitForChild("PickUpVitaminEvent")

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.E then
        PickUpEvent:FireServer(tool, adornee)
    end
end)

gui.Activated:Connect(function()
    PickUpEvent:FireServer(tool, adornee)
end)
-- Server Script
local PickUpEvent = game.ReplicatedStorage.PickUpVitaminEvent

PickUpEvent.OnServerEvent:Connect(function(playerWhoActivated, tool, adornee)
    if playerWhoActivated.Character and tool:IsA("Tool") and adornee:IsA("BasePart") then
        local magnitude = (playerWhoActivated.Character:WaitForChild("HumanoidRootPart").Position - adornee.Position).Magnitude
        print("Magnitude:", magnitude)
        
        if magnitude <= 25 then
            local clonedTool = tool:Clone()
            clonedTool.Parent = playerWhoActivated.Backpack
        end
    end
end)

These changes should help optimize and clarify the code. Make sure to replace placeholder values with the correct references to your objects.

  1. Exists
  2. Fixed those in the script the first dude made
    3.ill try that
1 Like

It seems like there might be an issue related to the use of WaitForChild within the local script, which could lead to an infinite yield situation. To resolve this, consider modifying the local script as follows:

-- Local Script (StarterPlayerScripts)
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local toolName = "Vitamins" -- Change to the name of your tool
local adorneeName = "Cylinder" -- Change to the name of your Adornee

local tool = nil
local adornee = nil

-- Function to find the tool and adornee
local function findToolAndAdornee()
    tool = workspace:WaitForChild(toolName, math.huge)
    if tool then
        adornee = tool:FindFirstChild(adorneeName)
    end
end

-- Call the function to find the tool and adornee
findToolAndAdornee()

local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local PickUpEvent = game.ReplicatedStorage:WaitForChild("PickUpVitaminEvent")

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.E then
        PickUpEvent:FireServer(tool, adornee)
    end
end)

Explanation:

  1. Instead of using WaitForChild directly in the script, a function findToolAndAdornee is created to locate the tool and adornee.
  2. The function is called immediately after defining it to ensure that tool and adornee are assigned before further use.
  3. The use of math.huge as the timeout in WaitForChild ensures that the script waits until the tool is found, preventing an infinite yield situation.

Try implementing these changes and see if it resolves the issue with the infinite yield and allows the InputBegan to print as expected.

My fingers hurt so much… Is it me or do I type a little TOO fast…?

(This took a long time to type since they still hurt like hell and the reply cooldown thangy thang)

I type 99-101 WPM so I feel ya. Removed the waitforchilds and got this VitaminBottle is not a valid member of Model “Workspace.Vitamins.VitaminBottle” (keep in mind I havent used the script above your message just yet.

this made no errors or warning but it does not work for some reason. I dont know why but roblox isnt on my side today.

Maybe try doing some edits to the script or scripts to make it work just well.

(My fingers still hurt…)

Quick note, you may need to edit the scripts another day since it’s a lot…

Most likely with the server script. I dont really know if its a backpack problem or a magnitude problem

1 Like

Uh does it have this boolen set to true on the textbutton?
image