Why won't this collide properly? Alt title: How should I fix these collisions?

I’m having trouble making a vehicle go realistically. By that I mean it shouldn’t let me drive through walls , etc. It only detects raycasts at certain rotations (??), when the only things that are blacklisted are characters and the cart itself.

RBXL Download

GolfCartTrouble.rbxl (190.2 KB)

My Script(s)
LocalScript

Located in game.StarterGui!

local cart = workspace.golfcart
local remote = game.ReplicatedStorage:WaitForChild('GolfCartRemote')

local uis = game:GetService('UserInputService')

local Forward = false
local Backward = false
local Left = false
local Right = false

local Inputs = {
	[Enum.KeyCode.W] = "W",
	[Enum.KeyCode.D] = "D",
	[Enum.KeyCode.A] = "A",
	[Enum.KeyCode.S] = "S",
}

uis.InputBegan:Connect(function(Input)
	if Inputs[Input.KeyCode] == "W" then
		Forward = true
	end
	
	if Inputs[Input.KeyCode] == "S" then
		Backward = true
	end
	
	if Inputs[Input.KeyCode] == "A" then
		Left = true
	end
	
	if Inputs[Input.KeyCode] == "D" then
		Right = true
	end
end)

uis.InputEnded:Connect(function(Input)
	if Inputs[Input.KeyCode] == "W" then
		Forward = false
	end
	
	if Inputs[Input.KeyCode] == "S" then
		Backward = false
	end
	
	if Inputs[Input.KeyCode] == "A" then
		Left = false
	end
	
	if Inputs[Input.KeyCode] == "D" then
		Right = false
	end
end)

game:GetService('RunService').Heartbeat:Connect(function()
	wait()
	if cart.MainSeat.Occupant ~= game.Players.LocalPlayer.Character:WaitForChild('Humanoid') then return end
	
	local rayOrigin = cart.DriveSeat.Position
	local rayDirection = (cart.DriveSeat.CFrame * CFrame.new(0,0,-5.5)).p
	
	-- Build a "RaycastParams" object and cast the ray
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {cart, game.Players.LocalPlayer.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	
	local Position = CFrame.new()
	local Turn = CFrame.new()
	
	cart.Taillight.Position = rayDirection
	
	if Forward then
		if raycastResult then
			if (rayDirection - raycastResult.Position).magnitude < 2.5 then return end
		end
		Position *= CFrame.new(0,0,-0.25)
	end	
	
	if Backward then
		Position *= CFrame.new(0,0,0.25)
	end	
	
	if Left and Forward then
		Turn *= CFrame.Angles(math.rad(0), math.rad(1), math.rad(0))
	end	
	if Left and Backward then
		Turn *= CFrame.Angles(math.rad(0), math.rad(-1), math.rad(0))
	end	
	
	if Right and Forward then
		Turn *= CFrame.Angles(math.rad(0), math.rad(-1), math.rad(0))
	end
	if Right and Backward then
		Turn *= CFrame.Angles(math.rad(0), math.rad(1), math.rad(0))
	end
	
	remote:FireServer(Position, Turn)
end)
Normal Script

Located in workspace.golfcart!

local remote = game.ReplicatedStorage:WaitForChild('GolfCartRemote')

remote.OnServerEvent:Connect(function(Player, Position, Turn)
	script.Parent:SetPrimaryPartCFrame(script.Parent.PrimaryPart.CFrame * Position * Turn)
end)
Issue Video

https://streamable.com/e/c2fgyh

Note that the headlights are in a weird spot because it’s where the RayDirection is.

Extra details about whats wrong

So my golf-cart that im scripting isn’t working properly; it shouldn’t be able to let you drive through walls, which worked… for a bit. Eventually I found out why it only took a half-n-hour to finish: You can still drive into things from different directions.

It makes more sense if you test it yourself or at least watch the video above.

This is really confusing; I did exactly what the devhub did, so I’ve got no clue why its not working.

Thanks for helping!

1 Like

I finally got it working by editing the localscript to

local crashraylength = 7.5

local cart = workspace.golfcart
local remote = game.ReplicatedStorage:WaitForChild('GolfCartRemote')

local uis = game:GetService('UserInputService')

local Forward = false
local Backward = false
local Left = false
local Right = false

local Inputs = {
	[Enum.KeyCode.W] = "W",
	[Enum.KeyCode.D] = "D",
	[Enum.KeyCode.A] = "A",
	[Enum.KeyCode.S] = "S",
}

uis.InputBegan:Connect(function(Input)
	if Inputs[Input.KeyCode] == "W" then
		Forward = true
	end
	
	if Inputs[Input.KeyCode] == "S" then
		Backward = true
	end
	
	if Inputs[Input.KeyCode] == "A" then
		Left = true
	end
	
	if Inputs[Input.KeyCode] == "D" then
		Right = true
	end
end)

uis.InputEnded:Connect(function(Input)
	if Inputs[Input.KeyCode] == "W" then
		Forward = false
	end
	
	if Inputs[Input.KeyCode] == "S" then
		Backward = false
	end
	
	if Inputs[Input.KeyCode] == "A" then
		Left = false
	end
	
	if Inputs[Input.KeyCode] == "D" then
		Right = false
	end
end)

game:GetService('RunService').Heartbeat:Connect(function()
	wait()
	if cart.MainSeat.Occupant ~= game.Players.LocalPlayer.Character:WaitForChild('Humanoid') then return end
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {cart, game.Players.LocalPlayer.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local ForwardRay = workspace:Raycast(cart.DriveSeat.Position, (cart.DriveSeat.CFrame * CFrame.new(0,0,-1000)).p, raycastParams)
	local BackwardRay = workspace:Raycast(cart.DriveSeat.Position, (cart.DriveSeat.CFrame * CFrame.new(0,0,1000)).p, raycastParams)	
	
	local Position = CFrame.new()
	local Turn = CFrame.new()
	
	if Forward then
		if ForwardRay then
			if ((cart.DriveSeat.CFrame * CFrame.new(0,0,-1000)).p - ForwardRay.Position).magnitude > (1000 - crashraylength) and not ForwardRay.Instance.Anchored then
				return
			end
		end
		Position *= CFrame.new(0,0,-0.55)
	end	
	
	if Backward then
		if BackwardRay then
			if ((cart.DriveSeat.CFrame * CFrame.new(0,0,1000)).p - BackwardRay.Position).magnitude > (1000 - crashraylength) and not BackwardRay.Instance.Anchored then
				return
			end
		end
		Position *= CFrame.new(0,0,0.25)
	end	
	
	if Left and Forward then
		Turn *= CFrame.Angles(math.rad(0), math.rad(2), math.rad(0))
	end	
	if Left and Backward then
		Turn *= CFrame.Angles(math.rad(0), math.rad(-2), math.rad(0))
	end	
	
	if Right and Forward then
		Turn *= CFrame.Angles(math.rad(0), math.rad(-2), math.rad(0))
	end
	if Right and Backward then
		Turn *= CFrame.Angles(math.rad(0), math.rad(2), math.rad(0))
	end
	
	remote:FireServer(Position, Turn)
end)

Imma take this topic down

1 Like