Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

1 comment

0

k no need for optimization ill do that later

This discussion is preserved as it was. New answers, comments, and votes are closed.