Is there a more reliable and precise way / alternative of using .Touched?

I have tried all that I possibly can, such as using Raycasts but I can’t manage to get the pies to properly weld on a surface without it floating, bouncing back and then welding or “teleporting” to the surface it’s supposed to weld to without even touching it

The problem:
image

1 Like

How are you doing raycasts for this? It should work fine as long as you set the pie’s position with the raycast’s position and then weld

1 Like

Here’s what I’ve tried with the raycast, it’s a modified version of the original script that uses the raycast instead of touched

local ts = game:GetService("TweenService")

local Pie = script.Parent

local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

local Creator = Pie:WaitForChild("creator")

local BodyForce = Pie:WaitForChild("BodyForce")
local BodyGyro = Pie:WaitForChild("BodyGyro")

local HitSound = Pie:WaitForChild("Hit")

local Stuck = false

local Damage = 0

local PieWeld

local function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

local function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

local function Stick(Object, Hit)
	local Weld = Instance.new("Weld")
	Weld.Part0 = Pie
	Weld.Part1 = Hit
	local HitPos = Pie.Position
	local CJ = CFrame.new(HitPos)
	Weld.C0 = Pie.CFrame:inverse() * CJ
	Weld.C1 = Hit.CFrame:inverse() * CJ + Vector3.new(0, 0, 0)
	Weld.Parent = Object
	return Weld
end

local function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

local function Touched(Hit)
	if not Hit or not Hit.Parent or Stuck then
		return
	end
	local character = Hit.Parent
	if Hit:FindFirstChild("UnStickable") then
		return
	end
	if character:IsA("Hat") or character:IsA("Tool") then
		character = character.Parent
	end
	local CreatorPlayer
	if Creator and Creator.Value and Creator.Value:IsA("Player") then
		CreatorPlayer = Creator.Value
	end
	if CreatorPlayer and CreatorPlayer.Character == character then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if CreatorPlayer and player and IsTeamMate(CreatorPlayer, player) then
		return
	end
	Stuck = true
	PieWeld = Stick(Pie, Hit)
	if HitSound then
		HitSound:Play()
	end
	for i, v in pairs({BodyForce, BodyGyro}) do
		if v and v.Parent then
			v:Destroy()
		end
	end
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid and humanoid.Health > 0 then
		UntagHumanoid(humanoid)
		if CreatorPlayer then
			TagHumanoid(humanoid, CreatorPlayer)
		end
		humanoid:TakeDamage(Damage)
	end
end

while true do
	wait()
	if BodyGyro and BodyGyro.Parent then
		local Direction = Pie.Velocity.Unit
		BodyGyro.MaxTorque = Vector3.new(1e4, 1e4, 1e4)
		BodyGyro.CFrame = CFrame.new(Vector3.new(0, 0, 0), Direction)
	end
	if PieWeld then
		break
	end

	local raycastResult = workspace:Raycast(Pie.Position, Pie.Velocity.Unit * 10)
	if raycastResult then
		Touched(raycastResult.Instance)
	end
end

wait(7)

if PieWeld and PieWeld.Parent then
	PieWeld:Destroy()
end

ts:Create(Pie,TweenInfo.new(.5,Enum.EasingStyle.Sine),{Transparency = 1}):Play()
Pie.CanCollide = false

image
This is one of the many results, it’s not touching the floor

You can pass the result of the raycast in the Touched function to use it for the pie

Touched(raycastResult.Instance, raycastResult)

and then set the parameter for the function

local function Touched(Hit, raycastResult)

Which would allow you to set the pie’s position

Pie.Position = raycastResult.Position
PieWeld = Stick(Pie, Hit)

This probably won’t look like how you want it to but you can change some things to fit your needs

1 Like

The problem with this is that the pie “teleports” to the surface its supposed to weld to and the orientation doesnt align with the surface it should stick to

image

I apologize for inconveniencing, I’m not really proficient at raycasting

It’s cool yeah thats what I was talking about, I’m not really sure what way is forward for the pie but you can try this out instead of positioning the pie you can set the CFrame to look at the direction of where it hit.

Pie.CFrame = CFrame.lookAt(raycastResult.Position, raycastResult.Position + raycastResult.Normal)
PieWeld = Stick(Pie, Hit)
1 Like

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