Hello, I’m making a simulator game involving paper airplanes,
Once I throw the airplane and it touches the ground it should wait 2 seconds and then be destroyed, then have a clone parent it to backpack. (Sorry if this is an inefficient way, i’m new)
this is my current code:
workspace.DescendantRemoving:Connect(function(part)
if part.Name == "PaperAirplane" then
print("parented")
ClonedTool.Parent = player.Backpack
end
end)
There is no error, and it gets destroyed as it should. Nothing prints either.
I’ve also tried this code:
script.Parent.Handle.Touched:Connect(function(hit)
print("hit")
if hit.Parent:FindFirstChild("Part") then
print("Is part")
wait(2)
script.Parent:Destroy()
local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()
ClonedTool.Parent = game.Players.LocalPlayer.Backpack
print("Destroyed")
end
end)
But it gives me this error: attempt to index nil with ‘Backpack’
game.Players.LocalPlayer doesnt exist in server scripts you should do it like this:
local isTouched=false
script.Parent.Handle.Touched:Connect(function(hit)
print("hit")
if isTouched then
return
end
if game.Players:FindFirstChild(hit.Parent.Name) then
isTouched = true
local player = game.Players:WaitForChild(hit.Parent.Name)
local ToolName = "PaperAirplane"
local source = game.ServerStorage
if source:FindFirstChild(ToolName) then
local backpack = player.Backpack
if backpack then
source[ToolName]:Clone().Parent = backpack
end
end
end
task.wait(1.25)
script.Parent:Destroy()
end)
local __CS = game:GetService("CollectionService")
script.Parent.Touched:Connect(function(__PART:BasePart?)
local __PLAYER = game:GetService("Players"):GetPlayerFromCharacter(__PART.Parent)
if __PLAYER ~= nil then
if not __CS:HasTag(__PLAYER, "__CD") then
task.delay(1,function()
__CS:RemoveTag(__PLAYER)
end)
local __TOOL = game:GetService("ServerStorage"):FindFirstChild("TOOLNAMEHRE"):Clone()
__TOOL.Parent = __PLAYER.Backpack
script.Parent:Destroy()
end
end
end)
local Handle = script.Parent.Handle
Handle.Touched:Connect(function(hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if (Player) then
script.Parent:Destroy()
local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()
ClonedTool.Parent = Player.Backpack
print("Destroyed")
end
end)
Hi. I want the airplane to be destroyed after it touches a part. Preferably after 2 seconds, then it creates a clone and parents it to the players backpack.
local Handle = script.Parent.Handle
Handle.Touched:Connect(function(hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if (Player) then
wait(2)
script.Parent:Destroy()
local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()
ClonedTool.Parent = Player.Backpack
print("Destroyed")
end
end)
Thats not how that works. You’d need a script inside the airplane when the Airplane collides with a part. Or do this
local Handle = script.Parent.Handle
Handle.Touched:Connect(function(hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if (Player) then
wait(2)
script.Parent:Destroy()
local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()
ClonedTool.Parent = Player.Backpack
ClonedTool.InsertPartToCollide.Touched:Connect(function(hit)
PartToDestroy:Destroy()
end)
print("Destroyed")
end
end)
I dont understand, sorry. I want it to detect when it touches a part and then destroys itself, then puts the clone into the players backpack. And rinse and repeat
So you don’t need to use game.Players.LocalPlayer. It’s used in LocalScript. Also, can you send the whole code, so that we could check where you defined a player ?