Interaction System Not working!

So right now im working on a client sided interaction system and when i click e even tho im not close to the part it still prints

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")


for _, part in pairs(workspace.Interactions:GetChildren()) do
	local pos = part.Position
	local distance = Player:DistanceFromCharacter(pos)
	
	UIS.InputBegan:connect(function(Input)
		local KeyCode = Input.KeyCode
		
		if KeyCode == Enum.KeyCode.E then
			if distance <= 5 then
				warn("Played Pressed E ")
			else
				
			
			end
		end
		
	end)
	
end

You’re only setting the distance property once, and so it’s checking for that same position each time which would result in the distance variable being greater than 5 each time

Implement that line inside the InputBegan event instead:

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")


for _, part in pairs(workspace.Interactions:GetChildren()) do
	local pos = part.Position

	UIS.InputBegan:connect(function(Input)
		local KeyCode = Input.KeyCode
		local distance = Player:DistanceFromCharacter(pos)
	
		if KeyCode == Enum.KeyCode.E then
			if distance <= 5 then
				warn("Played Pressed E ")
			else
				
			
			end
		end
		
	end)
	
end
2 Likes