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

Need help with player lock system,any help?

Im making a system which similar to the game soulshatter,after searching around and messing in my game,this is my current code:

local mouse = game.Players.LocalPlayer:GetMouse()
local runService = game:GetService("RunService")

local uis = game:GetService("UserInputService")
local Target = mouse.Target
local focusing = false
local realtar = nil
uis.InputBegan:Connect(function(input)
	if (input.KeyCode == Enum.KeyCode.L) then
		
		focusing = not focusing		
		if (not focusing) then
			game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
		else
		game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
		end
	end
end)
while game:GetService("RunService").RenderStepped:Wait() do
	if (focusing) then
		if mouse.Target ~= nil then
			local model = mouse.Target.Parent
			if model:FindFirstChildOfClass ("Humanoid") then
				print "Pointing at a model with Humanoid"
				for i,v in pairs(game.Workspace:GetDescendants()) do
			if v.Name == Target.Parent:FindFirstChild("HumanoidRootPart").Name then
				realtar = v.Parent.HumanoidRootPart
					end
				end
			end
		elseif mouse.Target.Parent:FindFirstChildOfClass("Humanoid") then
			for i,v in pairs(game.Workspace:GetDescendants()) do
				if v.Name == Target.Parent:FindFirstChild("HumanoidRootPart").Name then
					realtar = v.Parent.HumanoidRootPart
				end
			end
		end
		
					game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position,Vector3.new(realtar.Position.X,game.Players.LocalPlayer.Character.HumanoidRootPart.Position.Y,realtar.Position.Z))
					game.Workspace.CurrentCamera.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame*CFrame.new(Vector3.new(7.5,2.5,10))
	end
end

first it work fine if i only use the locking codes,not mouse check(in this case the target is a part),after adding some codes to check players or dummies,there is an error in output

Players.lamgogo.PlayerScripts.PlayerLockSystem:26: attempt to index nil with 'Parent'

im looking for any fixing answear or a new and better locking player code,any help will be much appreciate

3 answers

✓ Accepted answer

You used "Target" instead of "mouse.Target", and a variable named "Target" doesn't exist. A tip I can give you is, store the mouse target variable and use it as normally without saving it can have sudden changes between frames.

2 comments

0

first,at thefirst lines i wrote "local Target = ...",second,can u give me more explaination or a code sample?i dont know wdym of storing mouse target

0

oh so i get what u say of store the muse targeti actually use the spawn function so it will save the target,ty for helping

lol a note was added to my answer for not adding words.

basically you need to put the mouse.target inside the loop.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local mouse = LocalPlayer:GetMouse()
local runService = game:GetService("RunService")

local uis = game:GetService("UserInputService")
local focusing = false
local realtar = nil

uis.InputBegan:Connect(function(input)
	if (input.KeyCode == Enum.KeyCode.L) then
		focusing = not focusing
		if (not focusing) then
			workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
		else
			workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
		end
	end
end)

while focusing do
	local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
	
	if mouse.Target ~= nil then
		local Target = mouse.Target
		local model = Target.Parent
		
		if model:IsA("Model") and model:FindFirstChildOfClass("Humanoid") then
			print("Pointing at a model with Humanoid")
			for i, v in pairs(workspace:GetDescendants()) do
				if v == Target.Parent:WaitForChild("HumanoidRootPart") then
					realtar = v
				end
			end
		end
	end
	
	Character:PivotTo(CFrame.new(Character:GetPivot().Position,  Vector3.new(realtar.Position.X, Character:GetPivot().Position.Y, realtar.Position.Z))
	
                    game.Workspace.CurrentCamera.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame*CFrame.new(Vector3.new(7.5,2.5,10))
	
	workspace.CurrentCamera.CFrame = Character:GetPivot() * CFrame.new(7.5, 2.5, 10)
	
	runService.RenderStepped:Wait()
end

this is the code i madein case anyone in my situationXD

local mouse = game.Players.LocalPlayer:GetMouse()
local runService = game:GetService("RunService")

local uis = game:GetService("UserInputService")
local focusing = false
local realtar = nil
local locking = false
local function Lock(Target)
	while game:GetService("RunService").RenderStepped:Wait() do
		if focusing == true then
		locking = true
		game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position,Vector3.new(Target.Position.X,game.Players.LocalPlayer.Character.HumanoidRootPart.Position.Y,Target.Position.Z))
		game.Workspace.CurrentCamera.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame*CFrame.new(Vector3.new(7.5,2.5,10))
		end
	end
end
uis.InputBegan:Connect(function(input)
	if (input.KeyCode == Enum.KeyCode.L) then
			if mouse.Target.Parent:FindFirstChild("Humanoid") then
				spawn(function()
					Lock(mouse.Target.Parent.HumanoidRootPart)
				end)
	end
		focusing = not focusing	
		if (not focusing) then
			if locking == true then
				game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
				locking = false
			end
		else
			if locking == false then
				game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
			end
		end
	end
end)

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