I'm trying to make something that will spawn an explosion on the player if they stay in an area for too long, But it continues to explode after the set time even know im not in the area anymore, i thought the if statement (12) under the wait() would prevent this, any ideas?
local Part = script.Parent Part.Touched:Connect(function(object) if object.Parent:FindFirstChild("UpperTorso") then local player = object.Parent:FindFirstChild("UpperTorso").Parent local localplayer = game.Players:FindFirstChild(player.Name) local playerteam = localplayer.Team if playerteam.Name == "Axis" then print("Detect") wait(3) if object.Parent:FindFirstChild("UpperTorso").Parent.Name == player.Name then local explosion = Instance.new("Explosion", workspace) explosion.ExplosionType = Enum.ExplosionType.NoCraters explosion.Position = player.UpperTorso.Position explosion.DestroyJointRadiusPercent = 0 explosion.BlastPressure = 1 print("Boom!") else player = "Null" localplayer = "Null" playerteam = "Null" print("Null") return end end -- if playerteam end end-- object uppertorso function end end) -- function end
There are a few issues with what you're doing. Let me break it down for you.
Part.Touched:Connect(function(object) if object.Parent:FindFirstChild("UpperTorso") then local player = object.Parent:FindFirstChild("UpperTorso").Parent local localplayer = game.Players:FindFirstChild(player.Name) local playerteam = localplayer.Team if playerteam.Name == "Axis" then print("Detect") wait(3)
With this, I'm going to assume this is an R15 game only, In which using UpperTorso is okay. In my demonstration, I used HumanoidRootPart in which is valid for both R6 and R15.
also, I used
game:GetService("Players")
for the reason of game. Players could come up nil if the Players Service name was changed (Yes this is possible lol)
Another issue here is you are taking when they get detected and just waiting 3 seconds. What happens if they leave after 1.3 seconds? It Is still going to continue waiting that last 1.7 seconds.
Also, the reason why it continues to explode even if you are not in the area is that there is nothing stopping the function from firing again with each individual part on the player's character. (Left hand, left lower arm, left upper arm and so on) Basically, if the part is big enough this function will fire 16+ times ( R15 + HumanoidRootPart and then some due to touched being sensitive) Example -- this is just from spawning in and falling into the part
So what you are going to want to do is add something to prevent that, as I did in my example was just checking to see if the object that touched was named HumanoidRootPart after it checked to see if it was within the character.
That's about it. Here's my completed code for this. Please do yourself a favor and take this code to learn from. Little advice, don't just copy and paste it. Have it side by side of what you're doing and rewrite the entire thing. (It's only like 70 lines, lol you'll be fine)
local timeToDetonate = 3 local Players = game:GetService("Players") local part = script.Parent local alreadyWaiting = {} local function detionatePart(character) local explosion = Instance.new("Explosion") explosion.ExplosionType = Enum.ExplosionType.NoCraters explosion.Position = character.UpperTorso.Position --explosion.DestroyJointRadiusPercent = 0 explosion.BlastPressure = 1 explosion.Parent = game:GetService("Workspace") end local function startTimer(player,detonationTime) print("Been Detected") local begin = tick() local timePassed = 0 repeat timePassed = tick() - begin wait() until timePassed >= detonationTime or alreadyWaiting[player] == nil return timePassed >= detonationTime and true or false end local function resetWaiting(player) if player then if alreadyWaiting[player] then alreadyWaiting[player] = nil end end end part.Touched:Connect(function(object) if object.Name ~= "HumanoidRootPart" then return end local player = Players:GetPlayerFromCharacter(object.Parent) if player then local character = object.Parent local humanoid = character:FindFirstChildOfClass("Humanoid") if humanoid == nil or humanoid.Health <=0 then return end if player.Team.Name == "Axis" then if alreadyWaiting[player] ~= nil then return end alreadyWaiting[player] = true local detonate = startTimer(player,timeToDetonate) if detonate then detionatePart(character) resetWaiting(player) else print("you are safe") end end end end) part.TouchEnded:Connect(function(object) if object.Name ~= "HumanoidRootPart" then return end local player = Players:GetPlayerFromCharacter(object.Parent) resetWaiting(player) end) Players.PlayerRemoving:Connect(function(player) resetWaiting(player) end)
If you have any questions, PLEASE feel free to ask. Even feel free to message me on Roblox! It.
Also, if you check here, I have an Example Place That explains everything down to what "end" does. That place is where I took your code and wrote my own and even tested things.