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

I'm making a key press detector in a vehicleseat. Can someone help me fix it?

Whenever I run the game, it says "Attempt to connect failed: Passed value is not a function" (The script is inside the vehicleseat)


script.Parent.ChildAdded:connect(function(newChild)	
	if newChild:IsA("Weld") then
		if newChild.Part1.Name == "HumanoidRootPart" then
			local player = game.Players:GetPlayerFromCharacter(newChild.Part1.Parent)
			if (player) then
				function KeyPress(input, gameProcessed)
					if input.KeyCode == Enum.KeyCode.W then
						print("OMG W WAS PRESS ! !! ! !")
					end
				end
				
			end
		end
		
	end
	
end)


game:GetService("UserInputService").InputBegan:Connect(KeyPress)

1 answer

Remember that scripts are read from top to bottom. And remember the function you've connected to ChildAdded will only be called once a child is added.

Line 20 was executed before the event was fired, thus the global KeyPress wasn't declared.

Since this is a server script, remember that the server cannot, and most importantly should not, handle user input. It doesn't make sense for it to. The server has no keyboard or mouse, or anything like that, and how will the server know who inputted?

If you would like for the printing to occur only if the player is seated, check that the Humanoid.Seat property is equal to the seat you want.

-- Should be local script and in somewhere they can run in
local client = game:GetService("Players").LocalPlayer

-- just use a function literal since the code isn't reusable
game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
    if gpe or input.KeyCode ~= Enum.KeyCode.E then
        return
    end
    if client.Character.Humanojd.Seat == seat then
        print("E Pressed and seated")
    end
end)

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