Need help with press E to enter vehicle (CollectionService)

So I have these vehicles and I have a small part to act as a node for entering any of them. I made them act as a node so that’s where the E button is going to appear.

The problem is I assigned the “VehicleNode” tag in each node. When I tested in only shows up on one vehicle. The Hot Pink colored one. I don’t know why.

I did find some solutions but it didn’t match the wanted output. I don’t know what else to do.
I fire the event in a normal script with the localscript then the normal script picks it up then I can sit on one vehicle only.

MY CODE:

Localscript:

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local HumanoidRootPart = Player.Character:WaitForChild("HumanoidRootPart")
local UserInputService = game:GetService("UserInputService")


for _,VehicleNode in pairs(CollectionService:GetTagged("VehicleNode")) do
	wait()
	for i,VehicleSeat in pairs(CollectionService:GetTagged("VehicleSet")) do
		wait()
		-- Keypressed Binding Input and Functions--
		
		UserInputService.InputBegan:Connect(function(keyCode)
			if keyCode.keyCode == Enum.KeyCode.E then
				local Magnitude = (VehicleNode.Position - HumanoidRootPart.Position).Magnitude
				if Magnitude <= 5 then
					script.Parent.ImageButton.Pop:Play()
					VehicleNode.EnterVehicle.Event:FireServer(VehicleSeat)
				end
			end
		end)
		
		-- Magnitude
		while true do
			wait()
			local Magnitude = (VehicleNode.Position - HumanoidRootPart.Position).Magnitude
			if Magnitude <= 5 then
				local D3ToD2 = workspace.CurrentCamera:WorldToScreenPoint(VehicleNode.Position)
				script.Parent.ImageButton.Visible = true
				script.Parent.ImageButton.Position = UDim2.new(0,D3ToD2.X,0,D3ToD2.Y,0)
			elseif Magnitude >= 5 then
				script.Parent.ImageButton.Visible = false
			end
		end
	end
end

Normal Script:

local Event = script.Event

Event.OnServerEvent:Connect(function(player,VehicleSeat)
	VehicleSeat:Sit(player.Character.Humanoid)
end)

I put the normal script in each node.

If you have any ideas, please tell me! You got any better ideas in the script, do let me know. If any of you know how to fix the problem, tell me. It would be greatly appreciated! :slightly_smiling_face:

3 Likes

Hey so, let me try to help, I’m new at Dev Forum but if you see at line 3 it says "CollectionService:GetTagged(“VehicleSet”)… so VehicleSet or VehicleSeat or VehicleSit, it’s maybe that?, also why you need a in pairs for the seats if you already have the vehicle, so if you have the vehicle model you can get the Seat by vehicle Nodle or vehicle model, like for a,b in pairs(VehicleNode:GetChildren()) do
if b:IsA(“VehicleSeat”) or b:IsA(“Seat”) then
CollectionService:GetTagged(b)

Anyway! Hope I could help you!!

4 Likes

The “while … do” is preventing the script from moving forward and thus won’t check for any other vehicle seat. Consider placing it into a RunService event, using coroutines or using spawn to have loops like these run on their own and not affecting the script itself. Or you could make it one big loop that checks for the closest vehicle seat and handle it from there.

Some other nitpicking here, the Event does not have sanity checking (exploit prevention) so exploiters are going to be able to teleport with ease. wait() shouldn’t be used since it’s quite slow. Once again, I would recommend you to use RunService events as a replacement for wait().

5 Likes

I use a remote event to communicate local player in a normal script. So i can make local player compatible with a normal script

I want the while loop to check in every vehicle node. I also assign a vehicleset tag for the vehicleseats. So when i pick up the event, it will let me sit in the vehicle without the tag in the function parameters, it wont let me sit.

Yes, but having it in that way, without using the methods above mentioned earlier, would permanently loop between the while and the end, so the in pairs loop will never finish due to the while loop.

So the while loop should be changed like this:

RunService.Heartbeat:Connect(function()
local Magnitude = (VehicleNode.Position - HumanoidRootPart.Position).Magnitude
if Magnitude <= 5 then
local D3ToD2 = workspace.CurrentCamera:WorldToScreenPoint(VehicleNode.Position)
				script.Parent.ImageButton.Visible = true
script.Parent.ImageButton.Position = UDim2.new(0,D3ToD2.X,0,D3ToD2.Y,0)
elseif Magnitude >= 5 then
				 
    script.Parent.ImageButton.Visible = false
       end
end

Sorry for poor indentation. I am on mobile for the moment.

That could work.

30charsbelike

1 Like

Which is better? Heartbeat or RenderStepped ? Or what? Which is the best? Im still new for runservice

That’s mainly a question for another topic, but for a use case like this, it doesn’t really matter.

1 Like

For sure heartbeat is better? Because ive seen people use heartbeat often in runservice.

I made a whole video on this subject:

7 Likes

Oh no it matters very much. Don’t use RenderStepped. RenderStepped runs before a frame is rendered so long-running, yielding or expensive code can delay the execution of render ticks and cause noticeable performance issues.

RenderStepped should only be used in a small number of cases, chiefly updating the camera or updating something in tandem with the camera.

2 Likes

Yeah that did work. But i am using collectionservice for multiple vehicles

Then should I use Heartbeat? For good loop?

I haven’t had the time to look at your thread in depth so I don’t know if a loop is necessary at all or not. Generally though, it depends on your use case and what your aim is. Stepped will run before physics simulation and Heartbeat will run afterwards. I opt for Heartbeat unless I need something happening before physics get simulated (or updated).

Nope. I used runservice heartbeat. It still don’t work. Made the problem worse.

I used the runservice and it made it worse. Non of E button appear each in them.

RenderStepped is mostly used for Camera, so you should use heartbeat instead.

1 Like

What do you mean? why you need vehicles get tagged if you have seats?

1 Like