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)
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!
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”)
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:
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.
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.
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.
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
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.
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.
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.
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:
Instead of using WaitForChild directly in the script, a function findToolAndAdornee is created to locate the tool and adornee.
The function is called immediately after defining it to ensure that tool and adornee are assigned before further use.
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.
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.