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

Attempt to compare userdata with number FIX?

I made this script to make a intereact gui pop up when i walk near a door

local RunService = game:GetService("RunService")
local Character = game.Players.LocalPlayer.Character
local Door = workspace.Door

RunService.RenderStepped:Connect(function()
	local DoorFromPlayer = (Character.Head.Position - Door.Open.Position).Unit
	
	if DoorFromPlayer > .25 then
		print(Character.Name .. " is near the door")
		
	else
		print(Character.Name .. " is not near the door")
	end
end)

But it gives this error

15:51:12.919 - Workspace.NathantheCraziest.DoorOpenScript:8: attempt to compare number and userdata

2 answers

✓ Accepted answer

Problems

  • You are trying to compare a number with a Vector3
  • Use RunService.Heartbeat instead of RunService.RenderStepped since thats used for effects and cutscenes

Solutions

  • At line 6 i think you meant .Magnitude not .Unit
  • Replace .RenderStepped with .Heartbeat

Full script

local RunService = game:GetService("RunService")
local Character = game.Players.LocalPlayer.Character
local Door = workspace.Door

RunService.Heartbeat:Connect(function() -- Use Heartbeat
    local DoorFromPlayer = (Character.Head.Position - Door.Open.Position).Magnitude -- Use Magnitude

    if DoorFromPlayer > .25 then
        print(Character.Name .. " is near the door")

    else
        print(Character.Name .. " is not near the door")
    end
end)

It's better to use a loop rather than RunService. I also changed Head to HumanoidRootPart it's what I normally use in my scripts.

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local door = workspace.Door.Open
local closestDistance = 20

while true do
    wait(1)
    local distance = (char.HumanoidRootPart.Position - door.Position).magnitude
    if distance <= closestDistance then
        print(plr.Name.." is next to the door!")
    end
end

1 comment

0

You are actually wrong

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