InputBegan isn't working?

Hello Scripter,

So i got a script in ServerScriptService which loops trough the CollectionService, to check for any models with the Tag “Car”. If there is, there is a whole script which will make the car work on so on.

However, i wanted to be able to flip the car, when its fallen over. I added this code:

	local function resetCarOrientation()
		UserInputService.InputBegan:Connect(function(input, gameProcessed)
			if input.KeyCode == Enum.KeyCode.F and not gameProcessed then
				if car.PrimaryPart then
					local position = car.PrimaryPart.Position
					local currentOrientation = car.PrimaryPart.Orientation

					local resetCFrame = CFrame.new(position) * CFrame.Angles(math.rad(currentOrientation.X), math.rad(currentOrientation.Y), 0)

		
					car:SetPrimaryPartCFrame(resetCFrame)
				else
					warn("Something wrent horribly wrong!")
				end
			end
		end)
	end

However, i tried printing to see if anything happens when pressing F. Nothing happened.
My idea is, its because of the loop. However i cant get the car model an other way (I know) to make this car flip script work.

Any help appreciated!

Pressing input do not work in server side, it have to be made in client side using a local script in StarterPlayerScripts.

Is there any other way i can check if F is pressed, on the Server?

No, however you can use a remote event to communicate between client and server.

local script

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.F and not gameProcessed then
        RemoteEvent:FireServer(TheCarYouAreDriving)
	end
end)

Server Script

RemoteEvent.OnServerEvent:Connect(function(Player, car)
	if car.PrimaryPart then
		local position = car.PrimaryPart.Position
		local currentOrientation = car.PrimaryPart.Orientation
		local resetCFrame = CFrame.new(position) * CFrame.Angles(math.rad(currentOrientation.X), math.rad(currentOrientation.Y), 0)

		car:SetPrimaryPartCFrame(resetCFrame)
	else
		warn("Something wrent horribly wrong!")
	end
end

Okay so i ended up doing this:

A local script in a gui (Speedometer speed displayer)

UserInputService.InputBegan:Connect(function(key, gameProcessed)
	if key == Enum.KeyCode.F and not gameProcessed and not debounce then
		debounce = true
		EventsFolder.CarEvents.FlipCar:FireServer(car)
		
		task.wait(.5)
		debounce = false
	end
end)

The main car script:

EventsFolder.CarEvents.FlipCar.OnServerEvent:Connect(function(carToFlip)
			if carToFlip then
				if carToFlip.PrimaryPart then
					local position = carToFlip.PrimaryPart.Position
					local currentOrientation = carToFlip.PrimaryPart.Orientation

					local resetCFrame = CFrame.new(position) * CFrame.Angles(math.rad(currentOrientation.X), math.rad(currentOrientation.Y), 0)

		
					carToFlip:SetPrimaryPartCFrame(resetCFrame)
				else
					warn("Something wrent horribly wrong!")
				end
			end
		end)

Tho it is still not 100% working.

You forgot to add the player here, you can’t skip this argument, currently “carToFlip” count as the player

EventsFolder.CarEvents.FlipCar.OnServerEvent:Connect(function(player, carToFlip)
UserInputService.InputBegan:Connect(function(key, gameProcessed)
	print("Key", key, "pressed!")

I printed out what key actually is.
Screenshot 2024-08-26 160950

Thanks i will also change that

key.KeyCode == Enum.KeyCode.F
1 Like

Thanks @Crygen54 it now is working!

1 Like

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