Help with SCP 096 type script and values

Hello, I have written a script that basically gets the characters in which have “viewed” the dummy (in this case SCP 096). In creates an object value in a folder, the value assigned to the character that has viewed dummy. It works the first time however, after testing it out with multiple characters, multiple values are being created for the single character and floods the folder. I only want one value for each character that has viewed the dummy. I was thinking it was because the character value in the script is still assigned to the previous character however this is not the case. I’m genuinely stumped and don’t know how to fix it as I went through the script multiple times and everything seems to be fine. Is there something I’m doing wrong or is there a better way of achieving this?

Here is the portion of the script that I am having problems with.


function potTargets()
	for _, v in pairs(workspace:GetDescendants()) do
		if v:IsA("Humanoid") then
			if v.Name == "Humanoid" and v.Health > 0 then
				local rayOrigin = v.Parent.Head.Position
				local rayDirection =  v.Parent.Head.CFrame.LookVector * 1000
				local raycastParams = RaycastParams.new()
				raycastParams.FilterDescendantsInstances = {SCP.Head, SCP.HumanoidRootPart}
				raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
				local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
				if raycastResult then
					CanChase = true
					getCharacterViewing = v.Parent
				end
				if game.Players:GetPlayerFromCharacter(v.Parent) then
					if CanChase == true and PlayerViewing == true and game.Players:GetPlayerFromCharacter(v.Parent) == getPlayerViewing and getPlayerViewing.Character == getCharacterViewing then
						SCP.Agro.Value = true
					end
				else
					if CanChase == true then
						SCP.Agro.Value = true
					end
				end
				if SCP.Agro.Value == true then
					local targetNum = SCP.PlayerTargets:GetChildren()
					if #targetNum == 0 then
						if game.Players:GetPlayerFromCharacter(getCharacterViewing) then
							game.ReplicatedStorage.Viewed096Client:FireClient(getPlayerViewing)
						end
						local newVal = Instance.new("ObjectValue")
						newVal.Name = getCharacterViewing.Name
						newVal.Value = getCharacterViewing
						newVal.Parent = SCP.PlayerTargets
					elseif #targetNum > 0 then
						for _, p in pairs(SCP.PlayerTargets:GetChildren()) do
							if p:IsA("ObjectValue") then
								if getCharacterViewing ~= p.Value then
									if game.Players:GetPlayerFromCharacter(getCharacterViewing) then
										if getPlayerViewing == game.Players:GetPlayerFromCharacter(getCharacterViewing) then
											game.ReplicatedStorage.Viewed096Client:FireClient(getPlayerViewing)
										end
									end
									local newVal = Instance.new("ObjectValue")
									newVal.Name = getCharacterViewing.Name
									newVal.Value = getCharacterViewing
									newVal.Parent = SCP.PlayerTargets
								end
							end
						end
					end
				end
			end
		end
	end
end

while true do
	wait()
	potTargets()
end

Thanks!

4 Likes

Try detecting if a value is already made and if not, THEN make one. I do this by using FindFirstChild() and with if value == nil then make the value. I believe that if you want the value to change, just change the value and don’t make a new one.

If you don’t understand anything, here’s an example of what I usually do:

If you want to make a value only once:

local ValueDetect = YourObject:FindFirstChild(“YourValue”)
if ValueDetect == nil then
– Make
end

If you just want to change an existing value:

local ValueDetect = YourObject:FindFirstChild(“YourValue”)
if ValueDetect ~= nil then
– Change
end

I don’t want to change the value, I want one value for each character that has viewed the dummy as shown in the video but whenever I try to experiment with 2 characters, the folder that stores the values is flooded with multiple values all assigned to one character which is not what I want. Sorry if I worded it a bit confusing.