Is the function repeating itself or is it something else?

No matter what I try “Spared!” continues to run at least 5 times after “Spare Triggered” has already been activated once.

I don’t understand how if the function is only triggering once yet the player continues to lose gold multiple times; is there something I’m missing here? I’m stumped.

Script (Server)

game.ReplicatedStorage.Requests.Spared.OnServerEvent:connect(function(pl)
	print("Spared Triggered")

	local dPlayer = pl	
	
	local dFolder = dPlayer and game.ServerStorage.PlayerData:FindFirstChild(dPlayer.Name)	
	local ch = dPlayer.Character
	local dhum = ch:FindFirstChild("Humanoid")	
	local dfx = dhum:FindFirstChild("Effects")
	local dsp = dhum:FindFirstChild("Spare")
		for _,v in pairs(dfx:children()) do
			if v.Name == "InCombat" then
				v:Destroy()
		end
			local Spared = Instance.new("BoolValue",dfx)
			Spared.Name = "Spared"
			game.Debris:AddItem(Spared,15)

			local iFrame = Instance.new("BoolValue",dfx)
			iFrame.Name = "FullDefence"
			game.Debris:AddItem(iFrame,15)

			local toolinuse = Instance.new("BoolValue",dfx)
			toolinuse.Name = "ToolInUse"
			game.Debris:AddItem(toolinuse,15)

			dsp.Value = dsp.MinValue

			print("Spared! (NPC)")

			local takeGold = dFolder.Gold.Value*0.2
			dFolder.Gold.Value = dFolder.Gold.Value - takeGold

			spawn(function()
				game.ServerStorage.Requests.SaveData:Invoke(dPlayer,dFolder)
			end)
		end
	end)

Script (Local)

game:service("RunService").RenderStepped:connect(function()
	if dead then return end	
	
	if SP.Value == SP.MaxValue then
		game.ReplicatedStorage.Requests.Spared:FireServer(Player)
		SP.Value = SP.MinValue
	end

At a quick glance it looks to me like you accidently have code running in the for loop. I think its an accident because of how everything is indented.

I have tried Spare.Changed but that only yielded the same results.

Are you suggesting I should set up some sort of disconnection for the RenderStepped?

Try this,

game.ReplicatedStorage.Requests.Spared.OnServerEvent:connect(function(pl)
	print("Spared Triggered")

	local dPlayer = pl	

	local dFolder = dPlayer and game.ServerStorage.PlayerData:FindFirstChild(dPlayer.Name)	
	local ch = dPlayer.Character
	local dhum = ch:FindFirstChild("Humanoid")	
	local dfx = dhum:FindFirstChild("Effects")
	local dsp = dhum:FindFirstChild("Spare")
	for _,v in pairs(dfx:children()) do
		if v.Name == "InCombat" then
			v:Destroy()
		end
	end
	
	local Spared = Instance.new("BoolValue",dfx)
	Spared.Name = "Spared"
	game.Debris:AddItem(Spared,15)

	local iFrame = Instance.new("BoolValue",dfx)
	iFrame.Name = "FullDefence"
	game.Debris:AddItem(iFrame,15)

	local toolinuse = Instance.new("BoolValue",dfx)
	toolinuse.Name = "ToolInUse"
	game.Debris:AddItem(toolinuse,15)

	dsp.Value = dsp.MinValue

	print("Spared! (NPC)")

	local takeGold = dFolder.Gold.Value*0.2
	dFolder.Gold.Value = dFolder.Gold.Value - takeGold

	spawn(function()
		game.ServerStorage.Requests.SaveData:Invoke(dPlayer,dFolder)
	end)
end)

I can’t believe I’ve been felled by such a simple formatting issue…
So it does matter after all.

Thank you kind stranger.

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