What do you want to achieve?
I am currently making a hard-coded hockey game. When an offsides is called, it will delete the ball, and then respawn a new ball at center ice. But, when I respawn the ball at center ice, the touched function doesn’t recognize that the ball is apart of workspace despite me setting the new ball’s parent to workspace. Does anybody have any ideas?
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to look around on the Developer Hub, and I tried to place my function in different locations of my code. I will send the touched function that I have below.
game.Workspace:FindFirstChild("ball").Touched:Connect(function(hit)
print("I HAVE BEEN RAN!")
if hit.Parent.Name == "Stick" then
playerPossess = hit.Parent.Parent
print("I am ran 1")
if offsides then
print("I am ran 2")
for i,v in pairs(game.Players:GetChildren())do
print("I am ran 3")
if v.TeamColor == BrickColor.new(offsideTeam) then
game.Workspace.ball:Destroy()
offsides = false
gameActive = false
offsideTeam = ""
wait(20)
local warmupBall = game.ServerStorage.ball:Clone()
warmupBall.Parent = game.Workspace
warmupBall.Position = game.Workspace.Folder.Center.Position
gameActive = true
runClock()
end
end
end
end
end)
No errors, no nothing. It just isn’t able to detect a new ball.
Your sceipt doesn’t recognize the puck because it is in fact a new one. When you destroy the old puck you are still referencing it with .Touched event, and the script is still listening for any changes. Over time (matter of couple of seconds), scripts automatically switch to new copy in some cases, but not this one. The solution is to either teleport the puck back to where new one would be spawned (easy way), or create new one, but disconnect the old touch event connection and connect again.
EDIT
Perhaps add a small, anchored, invisible part on the field with collisions disabled. Put it where pucks should “respawn”. Each time you need them to return, set their CFrame to the CFrame of that part.
I didn’t have a chance to test it, so expect some possible minior corrections needed, otherwise, this should work:
local ball = workspace:FindFirstChild("ball")
local currentEvent
local function onOffside()
for i, v in pairs(game:GetService("Players"):GetChildren()) do
if (v.TeamColor == BrickColor.new(offsideTeam)) then
currentEvent:Disconnect()
ball:Destroy()
offsides = false
gameActive = false
offsideTeam = ""
wait(20)
local warmupBall = game:GetService("ServerStorage").ball:Clone()
warmupBall.Parent = workspace
warmupBall.Position = workspace.Folder.Center.Position
currentEvent = warmupBall.Touched:Connect(onHit)
gameActive = true
runClock()
end
end
end
local function onHit(hit)
if (hit.Parent.Name == "Stick") then
local playerPossess = hit.Parent.Parent
if (offsides) then onOffside() end
end
end
if (ball) then currentEvent = ball.Touched:Connect(onHit) end