Help Finding Player's distance From a Part

local Player = game.Players.LocalPlayer

game:GetService('RunService').Heartbeat:Connect(function()
	coroutine.wrap(function()
		local Character = Player.Character or Player.CharacterAdded:Wait()
		
		if (Character.HumanoidRootPart.Position - MiddlePos).Magnitude >= 10 then
			game:GetService('UserInputService').InputBegan:Connect(function(Input)
				if Input.KeyCode == Enum.KeyCode.Q then
					Tween1Open:Play()
				end
			end)
		end
	end)()
end)

You should’ve shown all the code before. You’re using UserInputService which is only available for the client. Meaning InputBegan wouldn’t be listened to for any player. Also, you don’t need to check if the UserInputType is Keyboard, since if the KeyCode is something, it already means they pressed a button.

1 Like

Its most likely because you only check it once.
Try using a loop, for example use runservice

local RunService = game:GetService("RunService")

Plrs[Plr].CharacterAdded:Wait()
local PlayerPos = Plrs[Plr].Character:WaitForChild("Torso", 5).Position

--// If it is a client script then use renderstepped, if it is a server script then use heartbeat
RunService.RenderStepped:Connect(function()
if (MiddlePos - PlayerPos).Magnitude <= Distance.Value then
--// RunCode
end
end)

Hope this helps! Any questions feel free to ask.

1 Like

That is true. User input needs to be gotten from the client. You should get the input and check distance on the client, tell the server, and the server verify the distance.

1 Like

Using the, “for i, Player in Plrs:GetPlayers() do” gives me an error.

“attempt to call a nil value”

1 Like

That is just pseudo code though it is mostly correct. You will have to type and format it yourself. Just that the logic from it. That should be for i,v in pairs(table) do end

1 Like