Hello, I’m making a tool that shoots multiple parts(50) in the direction of the mouse using a tween and the parts are supposed to be destroyed after either 2 seconds via game.Debris:add() have pasted or they hit an something that is not the player via Destroy:().
The issue I’m having is that the parts don’t get destroyed via Destroy:() after touching something. I don’t understand why.
LocalScript:
local player = PlayerService.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
local PelletBarrageTool = script.Parent
local PlayerMouse = player:GetMouse()
local Tool = "PelletBarrage"
local Active = false
PelletBarrageTool.Activated:Connect(function()
if not Active then
for x = 1,50 do
ToolRemoteEvent:FireServer(
Tool,
PlayerMouse.Hit.p
)
wait(0.05)
end
end
end)
Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local ToolRemoteEvent = ReplicatedStorage.Remotes.RemoteEvents.ToolRemote
ToolRemoteEvent.OnServerEvent:Connect(function(
player,
Tool,
MouseHit
)
local character = player.Character
local RootPart = character:FindFirstChild("HumanoidRootPart")
local humanoid = character:FindFirstChild("Humanoid")
local Pellet = Instance.new("Part", workspace.PlayerObjects)
Pellet.Name = player.Name.."'s Pellet"
Pellet.CanCollide = false
Pellet.Anchored = true
Pellet.Shape = "Ball"
Pellet.BrickColor = BrickColor.new("White")
Pellet.Size = Vector3.new(0.5, 0.5, 0.5)
Pellet.CFrame = RootPart.CFrame * CFrame.new(math.random(-6,6),math.random(4, 8),math.random(1,3))
Pellet.CFrame = CFrame.new(Pellet.Position, MouseHit)
local Distance = (Pellet.Position - MouseHit).Magnitude
local TimeSpeed = Distance / 50
local PelletLaunchTweenInfo = TweenInfo.new(TimeSpeed)
local PelletLaunchTween = TweenService:Create(
Pellet,
PelletLaunchTweenInfo,
{
Position = MouseHit
}
)
PelletLaunchTween:Play()
game.Debris:AddItem(Pellet, 2)
Pellet.Touched:Connect(function(Hit)
if Hit.Name ~= player.Name.."'s Pellet" then
if not (Hit.Parent:FindFirstChild("Humanoid") == humanoid) then
if game.Players:GetPlayerFromCharacter(Hit.Parent) then
local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
HitHumanoid:TakeDamage(1)
elseif Hit.Parent:FindFirstChild("Humanoid") then
local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
HitHumanoid:TakeDamage(1)
end
Pellet:Destroy()
end
end
end)
end)
This is weird then. If the Baseplate has CanTouch checked, then the pellet should be able to touch it. So, are you absolutely sure the Baseplate has CanTouch on?
Are there any errors in the console? Could you try moving the pellet:Destroy() out of the second if statement?
if Hit.Name ~= player.Name.."'s Pellet" then
if not (Hit.Parent:FindFirstChild("Humanoid") == humanoid) then
if game.Players:GetPlayerFromCharacter(Hit.Parent) then
local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
HitHumanoid:TakeDamage(1)
elseif Hit.Parent:FindFirstChild("Humanoid") then
local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
HitHumanoid:TakeDamage(1)
end
end
Pellet:Destroy()
end
did the console have any errors? If it didn’t, try printing everything that the pellet hit.
Pellet.Touched:Connect(function(Hit)
print(Hit.Name, Hit)
if Hit.Name ~= player.Name.."'s Pellet" then
if not (Hit.Parent:FindFirstChild("Humanoid") == humanoid) then
if game.Players:GetPlayerFromCharacter(Hit.Parent) then
local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
HitHumanoid:TakeDamage(1)
elseif Hit.Parent:FindFirstChild("Humanoid") then
local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
HitHumanoid:TakeDamage(1)
end
end
Pellet:Destroy()
end
end)
No, FindFirstChild only searches for the children of the object, not the descendants.
I have to go to bed now, stayed up late enough, and have school in a few hours, so I will try and help you later on (12-14 hours) if you are still having difficulties.