Help With Touch

I am trying to make a script where a part got to a part when touched. When I touch the part id does it but after that it does not work any more. I am wanting the script to detelct if the player touched it and not a part. How do I fix this

-- SERVER SRIPT LOCATED IN BALL
-- <<SERVICES>>
local TweenService = game:GetService("TweenService")

-- <<VARIABLE>
local ball = script.Parent
local targetA = workspace.TargetA

-- <<FUNCTIONS>>
local function onTouch(hit)
	local ply = hit.Parent
	if game.Players:GetPlayerFromCharacter(ply) then

		local cframeTween = TweenService:Create(ball, TweenInfo.new(1, Enum.EasingStyle.Quint), 
			{CFrame = CFrame.new(targetA.Position)})
		cframeTween:Play()

	else
		print("D")
	end
end

ball.Touched:Connect(onTouch)

This could help.

-- <<SERVICES>>
local TweenService = game:GetService("TweenService")

-- <<VARIABLE>
local ball = script.Parent
local targetA = workspace.TargetA

-- <<FUNCTIONS>>
local function onTouch(hit)
    local ply = hit.Parent; local plr = game.Players:GetPlayerFromCharacter(ply)
    if plr then

        local cframeTween = TweenService:Create(ball, TweenInfo.new(1, Enum.EasingStyle.Quint), 
            {CFrame = CFrame.new(targetA.Position)})
        cframeTween:Play()

    else
        print("D")
    end
end

ball.Touched:Connect(onTouch)

When I use that the part moves but it prints “D”

What’s the chance the part touches a hat and, therefore, hit.Parent is not the character Model?

-- <<SERVICES>>
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

-- <<VARIABLE>
local ball = script.Parent
local targetA = workspace.TargetA

-- <<FUNCTIONS>>
local function onTouch(hit: BasePart)
	local character = hit:FindFirstAncestorOfClass("Model")
	if not character or not Players:GetPlayerFromCharacter(character) then
		print("Not a character")
		return
	end
	
	local cframeTween = TweenService:Create(ball, TweenInfo.new(1, Enum.EasingStyle.Quint), {CFrame = CFrame.new(targetA.Position)})
	cframeTween:Play()
end

ball.Touched:Connect(onTouch)

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