Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to add a debounce so i dont have a million force fields on the player, wont work?

Asked by
OFF_S4LE 127
3 years ago

I'm not very good at coding and just looked into debounces as it was the solution to my neverending problem. Thing is, im not very good with them. Can someone please fix my script and explain to me what i did wrong? Here's my attempt if you can even call it one. No errors, nothing prints either.

while wait(0.1) do
local Teams = game:GetService("Teams")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
repeat wait() until char:FindFirstChild("Humanoid")
local human = char.Humanoid
local forcefield = Instance.new("ForceField")
debounce = false
if forcefield.Parent == char then
debounce = true 
end
if player.Team == Teams["Safe"] then
debounce = false
if forcefield.Parent == char then
print("like the game to be pog")
forcefield.Name = "NewForceField"
forcefield.Parent = player.Character

        end
    end
end

1 answer

Log in to vote
1
Answered by 3 years ago

A new instance is created every time the loop runs. the if statement is checking if the forcefield at that moment, has a parent of char.

Basically, check if the player has a forcefield initially.

And no, sorry, I won't optimize your script.

while wait(0.1) do
local Teams = game:GetService("Teams")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
repeat wait() until char:FindFirstChild("Humanoid")
local human = char.Humanoid
if player.Team == Teams["Safe"] then
  local forcefield = Instance.new("ForceField",char)
  elseif char:FindFirstChildWhichIsA("ForceField") then char:FindFirstChildWhichIsA("ForceField"):Destroy() 
end
end
end

I also assumed that when the user leaves the safe zone, their forcefield gets destroyed or something, added that in as well, you can make the rest of the changes. Also please use crap like .Died instead of a loop because thats more efficient.

0
k no need for optimization ill do that later OFF_S4LE 127 — 3y
Ad

Answer this question