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!