Effect not working

im making a game where the killer has a trail they make on the ground that gives poison but when you get it and it goes away you don’t get it again.

local BloodP = script.Parent.BloodP
BloodP.CFrame = script.Parent.CFrame * CFrame.new(0, 2, 0)
BloodP.Touched:Connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local buffs = player.PlayerGui.ScreenGui["Buffs/Debuffs"]

			if not buffs:FindFirstChild("Poison") then
				local Poison = game.ReplicatedStorage["Buff/Debuff"]:Clone()
				Poison.Parent = buffs
				Poison.Name = "Poison"
				Poison.BDtext.Text = "Poison"
				Poison.time.Value = 5
				Poison.amount.Value = 1
			end
		end
	end
end)

I still don’t understand how my solution from 2 weeks ago didn’t work. Is the player even touching BloodP the second time?

yes, the player touches it the second time but the effect doesn’t work, I’ve tested it and i think it might be the code kills itself or smth, the code works the first time its touched but right after the code kills itself and stops working.

Did you use my code?

I’ll add a bunch of prints. Can you send your output?

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)
	print(hit.Name)
	if hit.Name == "HumanoidRootPart" then
		local playerName = hit.Parent.Name
		print("Player name:", playerName)
		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 then print("No 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
		print(poisonSreenGUI)		

		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

			print("Initialized Poison")
			
			task.wait(5) -- I assume your "time" value is in seconds
			
			print("Finished wait")

			Poison:Destroy()

			print("Destroyed, ", Poison, " should be nil")
		end
	end
end)

the “print(poisonSreenGUI)” prints nil all the time even when you have the effect.
it might not see the player having it. and the thing works after you lose the effect (I think) it just doesn’t give the effect anymore.
(it also thinks the player never has the effect)
(the code is in a server script too if that changes anything)

Oh, I think you/I may have confused the buffs ScreenGUI with the Poison GUI object. Try 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)
	print(hit.Name)
	if hit.Name == "HumanoidRootPart" then
		local playerName = hit.Parent.Name
		print("Player name:", playerName)
		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 then print("No player") return end -- Make sure the player actually exists
		local buffsScreenGUI = player.PlayerGui.ScreenGui["Buffs/Debuffs"] -- Set this as a variable, no need to follow this complex index chain multiple times
		local defaultPoisonGUI = buffsScreenGUI:FindFirstChild("Poison")
		local oldPoisonGUI = buffsScreenGUI:FindFirstChild("NewPoison")
		print(oldPoisonGUI)	

		if not oldPoisonGUI then
			print(player)
			local Poison = defaultPoisonGUI:Clone()
			Poison.Parent = buffsScreenGUI
			Poison.Name = "NewPoison"
			Poison.BDtext.Text = "Poison"
			Poison.time.Value = 5
			Poison.amount.Value = 1

			print("Initialized Poison")
			
			task.wait(5) -- I assume your "time" value is in seconds
			
			print("Finished wait")

			Poison:Destroy()

			print("Destroyed, ", Poison, " should be nil")
		end
	end
end)

“attempt to index nil with ‘Clone’” on line 18

Whoops, I’m so confused at this point…

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

script.Parent.BloodP.Touched:Connect(function(hit)
	print(hit.Name)
	if hit.Name == "HumanoidRootPart" then
		local playerName = hit.Parent.Name
		print("Player name:", playerName)
		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 then print("No player") return end -- Make sure the player actually exists
		local buffsScreenGUI = player.PlayerGui.ScreenGui["Buffs/Debuffs"]
		local oldPoisonGUI = buffsScreenGUI:FindFirstChild("Poison")
		print(oldPoisonGUI)	

		if not oldPoisonGUI then
			print(player)
			local defaultPoisonGUI = game.ReplicatedStorage["Buff/Debuff"]
			local Poison = defaultPoisonGUI:Clone()
			Poison.Parent = buffsScreenGUI
			Poison.Name = "Poison"
			Poison.BDtext.Text = "Poison"
			Poison.time.Value = 5
			Poison.amount.Value = 1

			print("Initialized Poison")
			
			task.wait(5) -- I assume your "time" value is in seconds
			
			print("Finished wait")

			Poison:Destroy()

			print("Destroyed, ", Poison, " should be nil")
		end
	end
end)

I think that after its destroyed it still thinks the effect is there

and it also keeps running the touched thing over and over and i think the wait doesn’t stop the touched event from running.

That means that print(oldPoisonGUI) prints some non-nil value even after it prints “Destroyed”?

Yeah, that’s ok, that’s what it’s supposed to do.

it does keep printing non-nil values.

Sorry for really late response.

That means that you never destroyed the Poison.

Can you show a video of your code, you running the code, and the explorer showing the descendants of buffsScreenGUI?

i solved it already, it was because the server still thought it was there, i destroyed it on the server and it worked.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.