Hello, so I have a football game where you can throw footballs around. I want to make it so when a tool(football) is not in a player inventory after 10 seconds, it disappears. I am not the best at coding this.
Here is what I have:
Hello, so I have a football game where you can throw footballs around. I want to make it so when a tool(football) is not in a player inventory after 10 seconds, it disappears. I am not the best at coding this.
Here is what I have:
To delete it after 10 seconds, use this script. It should be a script
with the parent being the tool you want to delete:
wait(10)
script.Parent:Destroy()
Do I place this script under the tool?
Yes, you should. That is how the script will know what the tool is
I am running into an issue. It works perfectly, but if the player holds it in his hand for over 10 seconds it deletes in his hands. I want it so it only deletes after 10 seconds if no one is using it.
Ok, so you want it to despawn only if it’s not being held?
local timeWaited = 0
while wait(1) do
if script.Parent.Parent == game.Workspace then
timeWaited += 1
if timeWaited >= 10 then
script.Parent.Parent:Destroy()
end
else
timeWaited = 0
end
end
Do I replace the script with a local script? (heart if yes)
Just do a normal script, it should work
do these lines matter?
Sorry, I messed up. Add one more “end” to the last line:
local timeWaited = 0
while wait(1) do
if script.Parent.Parent == game.Workspace then
timeWaited += 1
if timeWaited >= 10 then
script.Parent.Parent:Destroy()
end
else
timeWaited = 0
end
end
And this script should still be under the tool? (heart is yes)
The football is not disappearing after 10 seconds
Is the tool in the workspace? I guess it is hard to make a script for you without a proper understanding how your entire football system works.
Sorry, the football is in replicated storage for important reasoning.
So I would assume I change Workspace to ReplicatedStorage in the script?
local Football = PATHTOFOOTBALLHERE
Football:GetPropertyChangedSignal("Parent"):Connect(function()
wait(10);
for i,v in next, game:GetService("Players"):GetPlayers() do
if Football["Parent"] ~= v["Character"] then
Football:Destroy()
end
end
end
Do I replace this with the script the person before said?
Up to you. This SHOULD detect when it’s been 10 seconds and there is still no parent that is a player.
But you can try it to see.
local football = football_path_here
local function DeleteThatFootball()
if football.Parent.ClassName == "Backpack" then
local counter = 0
repeat wait(1); counter = counter+1 until football.Parent.ClassName ~= "Backpack" or counter == 10
if football.Parent.ClassName == "Backpack" then
football:Destroy()
end
end
local deletecoroutine = coroutine.wrap(DeleteThatFootball)
football:GetPropertyChangedSignal("Parent"):Connect(function()
deletecoroutine()
end)
Untested
Some might say if I set this function as a coroutine, the counts will overlap and just delete the football at a random time. The repeat is set to end if the football’s parent changes and is no longer part of the backpack or the counter reaches 10, meaning the football has remained in the backpack, the IF statement following won’t pass (if football isnt in backpack) and the function will end.