Please help, i can't figure this out

I am trying to make an effect system and im trying to make it check for if the effect is already on the player and if it is then it doesn’t add it.

script.Parent.BloodP.CFrame = script.Parent.CFrame * CFrame.new(0, 2, 0)
while true do
	wait(0)
	script.Parent.BloodP.Touched:Connect(function(hit)
		if hit.Name == "HumanoidRootPart" then
			local player = hit.Parent.Name
			if game.Players:FindFirstChild(player).PlayerGui.ScreenGui["Buffs/Debuffs"]:FindFirstChild("Poison") then
					
			else
				print(player)
				local Poison = game.ReplicatedStorage["Buff/Debuff"]:Clone()
				Poison.Parent = game.Players:FindFirstChild(player).PlayerGui.ScreenGui["Buffs/Debuffs"]
				Poison.Name = "Poison"
				Poison.BDtext.Text = "Poison"
				Poison.time.Value = 5
				Poison.amount.Value = 1
			end
		end
	end)
end

it adds it the first time but when the effect ends you can’t get it again.

just add a false boolean, return if its true at the start of your code snippet, and set it to true at the end

this code killed my grandma

2 Likes

Please check if you destroy Poison object when the effect ends.

Well, as @Oceanblue857 said, you never remove the poison so it will always exist the second time. I assume you want to do this:

script.Parent.BloodP.CFrame = script.Parent.CFrame * CFrame.new(0, 2, 0)

-- Please DO NOT put a .Touched connection in a while true do statement, that's gonna become an insane memory leak
script.Parent.BloodP.Touched:Connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
		local playerName = hit.Parent.Name
		local player = game.Players:FindFirstChild(playerName) -- Ideally, you should create a "Players" variable at the start of your code with game:GetService()
		if not player return end -- Make sure the player actually exists
		local poisonScreenGUI = player.PlayerGui.ScreenGui["Buffs/Debuffs"]:FindFirstChild("Poison") -- Set this as a variable, no need to follow this complex index chain multiple times
		
		if not poisonScreenGUI then
			print(player)
			local Poison = game.ReplicatedStorage["Buff/Debuff"]:Clone() -- Again, for good code readability, you should really create a variable at the start of your code for ReplicatedStorage
			Poison.Parent = poisonScreenGUI
			Poison.Name = "Poison"
			Poison.BDtext.Text = "Poison"
			Poison.time.Value = 5
			Poison.amount.Value = 1
			
			task.wait(5) -- I assume your "time" value is in seconds
			
			Poison:Destroy()
		end
	end
end)

I also edited some stuff because it can be rewritten more cleanly and for better optimization.

the gui destroys itself when it is done
the gui isn’t there the second time, it just doesn’t work

can you rephrase this, I don’t know what you mean.

local db = false

script.Parent.BloodP.CFrame = script.Parent.CFrame * CFrame.new(0, 2, 0)
while true do
	wait(0)
	script.Parent.BloodP.Touched:Connect(function(hit)
        if db then return end
		if hit.Name == "HumanoidRootPart" then
			local player = hit.Parent.Name
			if game.Players:FindFirstChild(player).PlayerGui.ScreenGui["Buffs/Debuffs"]:FindFirstChild("Poison") then
					
			else
				print(player)
				local Poison = game.ReplicatedStorage["Buff/Debuff"]:Clone()
				Poison.Parent = game.Players:FindFirstChild(player).PlayerGui.ScreenGui["Buffs/Debuffs"]
				Poison.Name = "Poison"
				Poison.BDtext.Text = "Poison"
				Poison.time.Value = 5
				Poison.amount.Value = 1
			end

            db = true
		end
	end)
end

Does that mean that print(Player) doesn’t print the second time?

yes, thats what happens, it just thinks its still there when its not.