Tool not being given

Hey developers, I’m trying to make a system where if a player sits on a VehicleSeat then they are given a tool, but the it isn’t giving them the tool, please help!

Script;

local Player

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local Char = seat.Occupant
	Player = game.Players:GetPlayerFromCharacter(Char)
	print("New player")
end)

while true do
	if Player then
		local FindTool = Player.Backpack:FindFirstChildWhichIsA("Tool")
		if FindTool and FindTool.Name == "Paddle" then
			print("having tool")
			if FindTool.Equipped == true then
				UsingTool = true
				print("using tool")
			else
				UsingTool = false
				print("not using tool")
			end
		else
			game.ServerStorage.Paddle:Clone().Parent = Player.Backpack
			print("not having tool")
		end
	end
	task.wait()
end

You are trying to get the player here, but the game will consider it nil on other functions

But I have the variable above the function so that way everything can use it, or at least I thought that was how it works.

Try this:

local Player

function Occupant()
local Char = seat.Occupant
	plr = game.Players:GetPlayerFromCharacter(Char)
if plr then
	print("New player")
     return game.Players:GetPlayerFromCharacter(Char)
end
end

Player = Occupant()

seat:GetPropertyChangedSignal("Occupant"):Connect(Occupant)


-- Rest of the Code

I got this Example by testing this:

local Player -- Misleading Variable

function A()
	local i = 20
	if i == 20 then
		return 20 + 1 -- 21
	end
	
end

Player = A()

print(Player) -- Prints "21" due to the function returning it

This Should transfer the data to the Variable so it isnt lost
I am dumb tho so i may be wrong

This works partially, I have found that the occupant isn’t actually the character, it is the humanoid, meaning it needs to be occupant.Parent.

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