Car Spawning through walls

I’m Making a car game and it is done and tested, getting ready to release but I found this issue, when i go right up against a wall the car spawns through the wall, how would I check if there is a wall and not spawn the car

Here is the spawn script as well as a video with an example

function spawnCar(player, carName)
	local playerCars = DataStores:LoadModels(player)
	if table.find(playerCars, carName) then
		
		if spawnedCars[player] then
			local existingCar = spawnCarFolder:FindFirstChild(spawnedCars[player])
			if existingCar then
				local seat = existingCar:FindFirstChild("DriveSeat")
				if seat.Occupant then
					seat.Occupant.Sit = false 
					wait(0.5)
					
					existingCar:Destroy()
				else
					existingCar:Destroy()
				end
			end
		end
		
		local humanoidRootPart = player.Character.HumanoidRootPart
		local clonedCar = carFolder[carName][carName]:Clone()
		
		clonedCar.Parent = spawnCarFolder
		clonedCar.Name = player.Name.."'s "..carName
		
		clonedCar:PivotTo(player.Character:GetPivot() * CFrame.new(0, 10, -15))
		
		spawnedCars[player] = clonedCar.Name
		
		delay(1, function()
			
			clonedCar.DriveSeat:Sit(player.Character.Humanoid)
			
		end)
	end
end

robloxapp-20230703-1352204.wmv (4.9 MB)

Just raycast in front of the player and see if it hits a part and maybe you could even check the depth of the part too

1 Like

How would I go about using raycast

shoot a ray cast from the hrp to the lookvector of that hrp * 30 studs for example

You’ve made a whole car game but don’t know about raycasting… kinda suspicious
but anyway you do something like

local result = workspace:Raycast(origin, direction)
if result then
     if result.Instance == "blah" then
       blah blah blah
   end
end

shit i didn’t even notice he coded the car i just went straight to the replies lmao, kinda sus fr

1 Like

Several People Were Involved in scripting everything, but ill try this

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