Detect when another player touches part

Hi.

So I am making a ball for my game that uses Welds to place the ball just in front of the player. But when another player is touching the part, I want the weld of the current player to destroy and for a new weld to be created for the other player who touched the part, and for then to have complete NetworkOwnership of the part.

The current script:

local Players = game:GetService("Players")
local Part = script.Parent
local Cooldown = false


function SetUpWeld(Player , Part , BodyPart)
	if BodyPart:FindFirstChild("BallWeld") then return end
	local Weld = Instance.new("Weld")
	Weld.Name = "BallWeld"
	Weld.Part0 = Part
	Weld.Part1 = BodyPart
	Weld.Parent = BodyPart
	Weld.C0 = CFrame.new(0,0,3)
	BodyPart:SetNetworkOwner(Player)
end

Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if Player and not Cooldown then
		local Character = Player.Character
		local LeftLeg = Character:WaitForChild("Left Leg")
		SetUpWeld(Player , Part , LeftLeg)
		Cooldown = true
		task.wait(.2)
		Cooldown = false
	end
end)
local Players = game:GetService("Players")
local Part = script.Parent
local Cooldown = false


function SetUpWeld(Player , Part , BodyPart)
	if BodyPart:FindFirstChild("BallWeld") then return end
	local Weld = Instance.new("Weld")
	Weld.Name = "BallWeld"
	Weld.Part0 = Part
	Weld.Part1 = BodyPart
	Weld.Parent = BodyPart
	Weld.C0 = CFrame.new(0,0,3)
	BodyPart:SetNetworkOwner(Player)
end

Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if Player and not Cooldown then
		local Character = Player.Character
		local LeftLeg = Character:WaitForChild("Left Leg")
        Part:BreakJoints()
		SetUpWeld(Player , Part , LeftLeg)
		Cooldown = true
		task.wait(.2)
		Cooldown = false
	end
end)

Might be what you want

2 Likes

I’ll try this when i get home, thanks!