Character error on tool activation

goal

so the goal is to extend the grabpack’s left hand mesh forward by 20 studs using tweenservice on the trigger (left hand click) of the tool. the tool itself is a viewmodel created.

error

15:50:12.869 Players.smallkiwis.Backpack.GrabPack 2.0.LeftHandExtender:3: attempt to index nil with ‘Character’ - Server - LeftHandExtender:3
15:50:12.869 Stack Begin - Studio
15:50:12.869 Script ‘Players.smallkiwis.Backpack.GrabPack 2.0.LeftHandExtender’, Line 3 - Studio - LeftHandExtender:3
15:50:12.869 Stack End - Studio

code & info

Tween Code:

local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local uis = game:GetService("UserInputService")
local ViewModel
local tool = script.Parent
local Camera = workspace.CurrentCamera


tool.Activated:Connect(function()
	local Camera = workspace.CurrentCamera
	local part = Camera.ViewModel.GrabPack.Hand_BlueMesh
	local isGrabbing = false

	local tweenInfo = TweenInfo.new(0.45, Enum.EasingStyle.Linear)
	local goal = {
		Position = character.HumanoidRootPart.Position + (character.HumanoidRootPart.CFrame.LookVector * 20)
	}

	local tween = TweenService:Create(part, tweenInfo, goal)
	tween:Play()

	tween.Completed:Wait()

	if isGrabbing == false then
		local part = script.Parent
		local tweenInfo = TweenInfo.new(0.45, Enum.EasingStyle.Elastic)
		local goal = {
			Position = character.HumanoidRootPart.Position + (character.HumanoidRootPart.CFrame.LookVector * -20)
		}
		local tween = TweenService:Create(part, tweenInfo, goal)

		task.wait(0.2)
		tween:Play()
	end
end)

viewmodel code

local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local uis = game:GetService("UserInputService")

local ViewModel

local equipped = false

tool.Equipped:Connect(function()
	equipped = true
	game.ReplicatedStorage.ViewModel:Clone().Parent = Camera
end)

tool.Unequipped:Connect(function()
	equipped = false
	ViewModel = Camera.ViewModel
	ViewModel:Destroy()
end)

local swayAmount = .9
local swayCF = CFrame.new()
local lastCameraCF = CFrame.new()

RunService.RenderStepped:Connect(function()
	if player.Character.Humanoid.Health <= 0 then
		if Camera:FindFirstChild("ViewModel") ~= nil then
			workspace.Camera.ViewModel:Destroy()
		end
	end

	if equipped == true then
		if Camera:FindFirstChild("ViewModel") ~= nil then
			Camera.ViewModel:SetPrimaryPartCFrame(Camera.CFrame)
			for i, v in pairs(Camera.ViewModel:GetChildren()) do
				if v:IsA("BasePart") then
					v.CanCollide = false
				end
			end

			local rot = Camera.CFrame:ToObjectSpace(lastCameraCF)
			local X,Y,Z = rot:ToOrientation()
			swayCF = swayCF:Lerp(CFrame.Angles(math.sin(X) * swayAmount, math.sin(Y) * swayAmount, 0), .1)
			lastCameraCF = Camera.CFrame

			Camera.ViewModel:SetPrimaryPartCFrame(Camera.CFrame * swayCF)
		end
	end
end)

hand mesh family tree

image
image

Tutorials used:

help would very much be appreciated <3

You’re trying to get LocalPlayer in a server script, Server scripts can’t do that, you can get the player from server but you will need to get the character first, and by other means

If you know the hierarchy beforehand you can just do Script.Parent.Parent.Parent until you get character and then do game.PlayersGetPlayerFromCharacter(character)

And before you run into the same problem, you can’t use UserInputService in the server either

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.