Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

(ANSWERED)Attempt to index nil with Position when using magnitude?

So yeah.The error is in the title as well but "Attempt to index nil with Position". Im not too good at magnitude but I'm developing my scripting skills.

	local debounce = false
	local plr = game.Players.LocalPlayer
	local mouse = plr:GetMouse()
	local uis = game:GetService("UserInputService")
	local char = plr.Character:FindFirstChild("HumanoidRootPart")
	local dis = 15
	uis.InputBegan:Connect(function(Input)
		local key = Input.KeyCode
		if key == Enum.KeyCode.E then
			if mouse.Target.Name == "Door" then
				if debounce == false then
					if (char.Position - mouse.Target.Position).Magnitude < dis then
					if mouse.Target.Value.Value == false then
						mouse.Target.Value.Value = true
						mouse.Target.Transparency = 1
					elseif mouse.Target.Value.Value == true then
						    mouse.Target.Value.Value = false
							mouse.Target.Transparency = 0
					    end
					end
				end
			end
		end
	end)

1 answer

✓ Accepted answer

for the char try waiting for it rather than finding it, also maybe change the name as it can get kinda confusing

local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")


if (hrp.Position - mouse.Target.Position).magnitude < dis then --replace line 12 with this

1 comment

0

Worked.Thank you very much.I will accept your answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.