So, i started making a roblox game, in which you survive a tornado, or you can chase storms, pretty similar to “Twisted”. And I made a system which prevents from getting hurt INSIDE a building, it’s a part which just sets the bool value inside a player to true, and if the tornado will run above the building(it needs to be an EF0 or EF1, else it will just kill you) and detect the value is set to true, it won’t hurt you. That part has a script, in which are two events, TOUCHED and TOUCHENDED. They just randomly fire when not supposed to, especially TOUCHENDED. Here’s the code i wrote:
local debounce = false
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if debounce == false then
if player then
debounce = true
local Indoor2 = player:WaitForChild("Indoor")
Indoor2.Value = true
end
wait(0.1)
debounce = false
end
end)
local debounce2 = false
script.Parent.TouchEnded:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if debounce2 == false then
if player then
debounce = true
local Indoor2 = player:WaitForChild("Indoor")
Indoor2.Value = false
end
wait(0.1)
debounce = false
end
end)
I also tried using region3 and magnitude, but this part is not a regular shape and there are many different houses across the map. Thanks for help and sorry for my english.
Where is this script located in? Is this the tornado or the safety part?
And please populate this script with print(flags) so we know what does and doesn’t run and when.
Thanks
So, i don’t really see anything wrong in the output about this script, i think it’s the touch ended problem, every one says that it’s really buggy and I should not use it.
Read about WorldRoot (workspace) to learn about the GetPartsInPart function, which returns a table. Loop through this table and check if the part is a limb in a character.
I dont think this would be feasible since you would need to constantly check GetPartsInPart…which would probably mean using RenderStep or a tick loop. Very performance heavy
If people are saying its buggy, then you might have to go and use that GetPartsInPart function. Perhaps attaching a tick function to spawn() and running that whenever Touched() properly finds something. And when GetPartsInPart cant find it, you can immediately turn off the tick function.
It still very bad for performance, but I dont really see an easy option if roblox code is what causes problems
I havent tried it myself, but try this out and see how it goes
local debouncer= false
local SpawnedThread = nil
--Run this constantly as long as it thinks the player is touching it
local GetParts = function(hit)
local PlayerModel = hit.Parent
while true do
wait()--you can modify how often it checks if the player is still touching here
local AllTouchingParts = workspace:GetPartsInPart(script.Parent)
local Flag=false
for __,element in pairs(AllTouchingParts) do
if element.Parent == PlayerModel then
--Player is still touching
Flag=true
end
end
if Flag then
--Player is no longer within safety zone
game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Indoor").Value=false
if SpawnedThread then
SpawnedThread:close()
SpawnedThread=nil
end
return
end
end
end
script.Parent.Touched:connect(function(hit)
if not debouncer then
debouncer=true
if SpawnedThread then
SpawnedThread:close()
SpawnedThread=nil
end
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local Indoor2 = player:WaitForChild("Indoor")
Indoor2.Value = true
SpawnedThread = task.spawn(GetParts,hit)
end
debouncer=false
end
end)
local debouncer= false
local SpawnedThread = nil
--Run this constantly as long as it thinks the player is touching it
local GetParts = function(hit)
local PlayerModel = hit.Parent
while true do
wait()--you can modify how often it checks if the player is still touching here
local AllTouchingParts = workspace:GetPartsInPart(script.Parent)
local Flag=false
for __,element in pairs(AllTouchingParts) do
if element.Parent == PlayerModel then
--Player is still touching
Flag=true
end
end
if Flag then
--Player is no longer within safety zone
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local Indoor2 = player:WaitForChild("Indoor")
Indoor2.Value = false
SpawnedThread = task.spawn(hit)
end
game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Indoor").Value=false
if SpawnedThread then
SpawnedThread:close()
SpawnedThread=nil
end
return
end
end
end
script.Parent.Touched:connect(function(hit)
if not debouncer then
debouncer=true
if SpawnedThread then
SpawnedThread:close()
SpawnedThread=nil
end
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local Indoor2 = player:WaitForChild("Indoor")
Indoor2.Value = true
SpawnedThread = task.spawn(GetParts,hit)
end
debouncer=false
end
end)