So i'm making a looting system and i have it so a lootbag is being cloned to the killed's position and when the player's torso's magnitude is less then 10 or 10 then the lootframe is visible otherwise it's not but it just flashes the lootframe when multiple enemies is killed why? And there is no errors and everything works if it wouldent start flashing if multiple enemies is killed

Here is the server script:

Remotes["TakeDamage"].OnServerEvent:connect(function(player, hit, g, dmg)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:FindFirstChild("Humanoid"):TakeDamage(dmg)
	end
	hit.Parent:FindFirstChild("Humanoid").Died:connect(function()
		local lootbag = game.ReplicatedStorage.Lootbag:Clone()
		lootbag.Parent = hit.Parent:FindFirstChild("Lootbags")
		lootbag.Name = "Lootbag "..hit.Parent.Name
		lootbag.CFrame = CFrame.new(hit.Parent:FindFirstChild("Torso").Position)
		
		local playerGui = player.PlayerGui
		local LootFrame = playerGui:WaitForChild("ScreenGui").LootFrame
		local looted = false
		LootFrame.LootButton.MouseButton1Click:connect(function()
			looted = true
			lootbag:Destroy()
		end)
		while true do
			if (player.Character.HumanoidRootPart.Position - lootbag.Position).magnitude <= 10 then
				LootFrame.Visible = true
				LootFrame.Frame.TextLabel.Text = lootbag.Name
				LootFrame.Position = UDim2.new(0.5, 0, 0.5, 0)
				if looted then
					break
				end
			else
				LootFrame.Visible = false
			end
			wait()
		end
	LootFrame.Visible = false
	end)
end)

2 comments

0

What is that sepose to do nothing is wrong with the CFrame