Camera offset script not working for some wild reason!

I am creating a third-person for a tool. When the character equips the weapon and presses the key E it should offset the camera to the side of the character and be able to toggle. I have created the script which fires the client to a local script which begins the offset, but when I run, it declares that Player = nil, I have attempted to use :GetPlayerFromCharacter but it has failed. This scripts parent is the tool itself, so it should be detecting the character and getting the player. Could someone explain or tell me what is wrong with this script?

local Blaster = script.Parent
local Player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
print(Player)
local Mouse = Player:GetMouse()
local Humanoid = Player.Character:WaitForChild("Humanoid")
Equipped = false
Toggle = true

Blaster.Equipped:Connect(function()
	Equipped = true
end)

Blaster.Unequipped:Connect(function()
	game.Workspace.WSS.cameraDisable:FireClient(Player)
	Equipped = false
end)

Humanoid.Died:connect(function()
	game.Workspace.WSS.cameraDisable:FireClient(Player)
	Equipped = false
end)

Mouse.KeyDown:connect(function(Key)
	if Equipped == true then
		if string.lower(Key) == "f" then
			if Toggle == true then
				Toggle = false
				game.Workspace.WSS.CameraEnable:FireClient(Player)
			elseif Toggle == false then
				Toggle = true
				game.Workspace.WSS.cameraDisable:FireClient(Player)
			end
		end
	end
end)

Any help would be much appreciated!

GetPlayerFromCharacter is used for the character model in workspace when the game begins, you are trying to get it through the player service. Quick question, is this to every player when they recieve the tool, or certain people?

3 Likes

A tool is directly under the character only when it is equipped, else it is placed in the backpack. So you’re affectively using GetPlayerFromCharacter with a backpack – which should error, not sure if you forgot to tell us.

Since this is a local script, you can use the LocalPlayer property of the Players service:

local Blaster = script.Parent
local Player = game.Players.LocalPlayer
print(Player)

I’m also not sure if characters load before or after tools are added to the backpack. If not loaded before, you’ll also need a line like this:

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid", 5)

The Tool class has a boolean Tool.Equipped property which you can use instead of using these connections:

if Blaster.Equipped then
1 Like

This is not a local script it fires the client into a local script.

Just certain players which have to tool.

Oh, my mistake. You can directly get the player then, since the tool would be placed in the backpack and the backpack is a child of the player object

local Player = script.Parent.Parent

Edit:
Wrong hierarchy, forgot to account for the script’s .Parent as well, @Vortbloxian gives the corrected line below

Isn’t it meant to be

local Player = script.Parent.Parent.Parent

Because it is placed in the player’s Backpack, not the player

3 Likes