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)
-- <<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)
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)