Hello developers,
I’m working on a part detection system, and basically, I want players to be able to interact with them. I have code below but, instead of the newInput
function being called when they are in the radius of the intValue
(which is set to 10). But, basically, whenever a player pressed any key, it runs the function. When I’m in the radius and press the key for PC (E) it does nothing and just prints received input
. How can I fix this? Thank you for your help if you are able to help, if not, have a great rest of your day!
local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local humanoid = script.Parent:WaitForChild("Humanoid")
local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local runService = game:GetService("RunService")
local function newInput(input, gameProcessed, key)
print("received input")
if not gameProcessed and key ~= nil and input.KeyCode == key.Value then
print("interacted with part! Key: " .. key)
end
end
runService.Heartbeat:Connect(function()
for i,v in pairs(game.Workspace.buildings.other.sled:GetChildren()) do
if v.ClassName == "Configuration" and v.Name == "settings" then
if (humanoidRootPart.Position - v.Parent.PrimaryPart.Position).Magnitude <= v.distance.Value and v.interacting.Value == nil and v.canInteract.Value == true then
-- print("new interaction: " .. v.Parent.Name)
v.interacting.Value = script.Parent
newInput(v.pc, v.xbox)
print(v.pc, v.xbox)
elseif (humanoidRootPart.Position - v.Parent.PrimaryPart.Position).Magnitude >= v.distance.Value and v.interacting.Value == script.Parent then
v.interacting.Value = nil
print("interaction lost with " .. v.Parent.Name)
end
end
end
end)
userInputService.InputBegan:Connect(newInput)