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

why does my handcuffs keep on saying nil in the output?

player = game.Players.LocalPlayer
character = player.Character
mouse = player:GetMouse()

script.Parent.Equipped:connect(function()
	mouse.Button1Down:connect(function(click)
		local distance = (character.Torso.CFrame.p - click.Torso.CFrame.p).magnitude
		if click:FindFirstChild("Humanoid") and click:FindFirstChild("Torso") then
			if distance < 21 then
				click.Torso.CFrame = CFrame.new(workspace.prison.Position)
			end
		end
	end)
end)

when i click on something that has a humanoid and a torso it keeps on saying this in the output:

16:34:14.292 - Players.EXpodo1234ALT.Backpack.Handcuffs.LocalScript:7: attempt to index local 'click' (a nil value)

1 answer

✓ Accepted answer

###This is because Button1Down passes no parameters, so click is nil. Remove that parameter. If you wanted to get the player like this, use Mouse.Target. Also, all Mouse events have been superseded by UserInputService.

local players = game:GetService("Players")
local plr = players.LocalPlayer -- use more local variables.
local char = plr.Character or plr.CharacterAdded:Wait()
local playerMouse = plr:GetMouse()
local UIS = game:GetService("UserInputService")
local tool = script.Parent
local HRP = char.HumanoidRootPart -- use this instead of torso. R6 and R15 have this
local equipped = false

tool.Equipped:Connect(function(mouse)
    equipped = true
end)

UIS.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 and equipped then
        local target = mouse.Target
        local targetPlr = players:GetPlayerFromCharacter(target.Parent)

        if target and targetPlr ~= plr then -- make sure we can't arrest ourselves!
            local difference = (HRP.CFrame.p - targetChar.HumanoidRootPart.CFrame.p)
            
            if difference.Magnitude <= 21 then
                targetPlr.Character.HumanoidRootPart.CFrame = game.Workspace.prison.CFrame
            end
        end
    end
end)

1 comment

0

I don't see no targetChar variable

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