Script first time works correctly but second times gets wrong position

Hello, I want script that will make dissapear a object when player will active a proximity prompt and then spawn on the position of players death

the problem is that second time player will die it give some weird position and I am not sure what these mean

I am not really sure how to fix this



local Inteligence = workspace.Gameplay:FindFirstChild("Inteligence") -- inteligence location
local TargetPlr = nil-- location of player with intelligence
local char = nil
function PlayerGlow(plr)	-- glow up the player with intelligence
	local Highlight = Instance.new("Highlight")
	Highlight.Parent = plr.Character
end

function HideInteligence()
	for i, v in ipairs(Inteligence:GetChildren()) do
		v.Transparency = 1
	end
	Inteligence.Main.ProximityPrompt.Enabled = false
end

Inteligence.Main.ProximityPrompt.Triggered:Connect(function(plr) -- functions which will proceed after player will take the intelligence
	PlayerGlow(plr)
	HideInteligence()
	TargetPlr = plr
	char = plr.Character
	char:FindFirstChild("Humanoid").Died:Connect(function() -- function which will trigger after dies 
		print("player diednooooo")
		char:FindFirstChild("Highlight"):Destroy()
		print("no more glow")
		Inteligence.Main.ProximityPrompt.Enabled = true
		Inteligence:MoveTo(char:FindFirstChild("UpperTorso").position) -- THIS PART OF THE SCRIPT WILL CHANGE THE POSITION
		TargetPlr = nil
		for i, v in ipairs(Inteligence:GetChildren()) do
			print("hell yeah guess who is back")
			v.Transparency = 0
			v.Anchored = false
		end
	end)
end)

game:GetService("RunService").Heartbeat:Connect(function()
	if TargetPlr ~= nil then
		TargetPlr.CharacterAdded:Connect(function(charr)
			char = charr
		end)
	end
end)

also, all the debug stuff got printed. everything works only the position is weird, like its not even an position of the original position
also, the position is not 0,0,0

2 Likes

What do you mean the second time the player dies? Is it not only supposed to listen for one death per time the prompt is triggered?

2 Likes

well, first time it will normally spawn on where player died but second time it will just teleport somewhere underground

1 Like

This is probably due to the character having not loaded yet. Try adding

local new_char = player.CharacterAdded:Wait()

before

Inteligence:MoveTo(char:FindFirstChild("UpperTorso").position) -- THIS PART OF THE SCRIPT WILL CHANGE THE POSITION

So it should look like

Inteligence:MoveTo(new_char:FindFirstChild("UpperTorso").position) -- THIS PART OF THE SCRIPT WILL CHANGE THE POSITION
1 Like

didnt helped, I have noticed that it will move on my position but with weird offset, no idea why

You are connecting event every time player triggers a prompt. So if you trigger the prompt 10 times, .Died event will not run once!

Also

What is this for?? Again, you are connecting ~60 events per second, forever.

and why its pproblem that I connect one function multiple times?

also, when prompt god trigered, it also get deleted until player dies

You should try CFrame instead of position