Detect when a value is removed from a player

What do you want to achieve?
I want to detect when a value is removed from the player.

Local Script:

local player = game.Players.LocalPlayer
local event = game.ReplicatedStorage.Sacrifice
local UIS = game:GetService("UserInputService")
local cd = false
local mouse = player:GetMouse()
local effect = game.Lighting.SacrificeEffect


mouse.KeyDown:Connect(function(key)
	key = key:lower()
	if key == 'h' and cd == false and not player.Character:FindFirstChild("Sacrificed") then
		cd = true
		event:FireServer()
		wait(2.2)
		effect.Enabled = true
		wait(.1)
		effect.Brightness = 0.2
		wait(.1)
		effect.Brightness = 0
		wait(.1)
		effect.Enabled = false
		effect.Brightness = 0.2
		cd = false
		player.Character.Changed:Connect(function()
			if not player.Character:FindFirstChild("Sacrificed") then
				effect.Enabled = true
				wait(.1)
				effect.Brightness = 0.2
				wait(.1)
				effect.Brightness = 0
				wait(.1)
				effect.Enabled = false
				effect.Brightness = 0.2

Server Script

local event = game.ReplicatedStorage.Sacrifice
local anim = script.Animation


event.OnServerEvent:Connect(function(player)
	local char = player.Character
	local hum = player.Character:WaitForChild("Humanoid")
	local energy = hum:WaitForChild("sacEnergy")
	local maxenergy = hum:WaitForChild("MaxSac")
	local loadanim = hum:LoadAnimation(anim)
	if not player.Character:FindFirstChild("Sacrificed") then
	loadanim:Play()
	wait(1)
	char.LowerTorso.Dagger.Transparency = 1
	char.RightHand.Dagger.Transparency = 0		
	wait(1)
	hum:TakeDamage(10)	
	local val = Instance.new("BoolValue")
	val.Name = "Sacrificed"
	val.Parent = char
	if energy.Value < maxenergy.Value then	
	energy.Value += 35
	wait(3)
	char.LowerTorso.Dagger.Transparency = 0
	char.RightHand.Dagger.Transparency = 1	
	wait(15)
	val:Destroy()
	loadanim:Stop()
			
		end
	end
end)

I’m not getting any errors in output. Thank you for any help.

If I understand you correctly,

Have you tried using ChildRemoving ? It fires when a child is removed from its parent.

2 Likes

No I haven’t tried it, but I’m not sure how to use it correctly. Could you help please?

Is it a value like an int or string?

1 Like

it’s a bool value that is put into the player’s character called “Sacrificed”

Value.Changed:Connect(function(value)
      print(value) —value is the new value 
end)

1 Like

I remove the value after 15 seconds. I’m trying to detect when the value is removed, I’m not sure this would work.

Edit: I tried this and it didn’t work, I didn’t get anything in output though.

Sorry if my intention wasn’t clear, I’m trying to enable an effect for the client after a value is removed from their player.

I tried “ChildRemoved” and it worked, thank you!

Script:

local player = game.Players.LocalPlayer
local event = game.ReplicatedStorage.Sacrifice
local UIS = game:GetService("UserInputService")
local cd = false
local mouse = player:GetMouse()
local effect = game.Lighting.SacrificeEffect


mouse.KeyDown:Connect(function(key)
	key = key:lower()
	if key == 'h' and cd == false and not player.Character:FindFirstChild("Sacrificed") then
		cd = true
		event:FireServer()
		wait(2.2)
		effect.Enabled = true
		wait(.1)
		effect.Brightness = 0.2
		wait(.1)
		effect.Brightness = 0
		wait(.1)
		effect.Enabled = false
		effect.Brightness = 0.2
		cd = false
		player.Character.ChildRemoved:Connect(function()
			if not player.Character:FindFirstChild("Sacrificed") then
				effect.Enabled = true
				wait(.1)
				effect.Brightness = 0.2
				wait(.1)
				effect.Brightness = 0
				wait(.1)
				effect.Enabled = false
				effect.Brightness = 0.2
			end
		end)
	end
end)
1 Like

I believe I have your answer, you would use ChildRemoved and :IsA() to detect if it was a value that was removed.

1 Like