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!
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!!
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().
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.
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.
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.
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).