The following script is just me testing projectiles with Bezier curves. It spawns at your character's HumanoidRootPart location and travels to your mouse's position. Everything works, but when I reset or when my character dies, the localscript is using the location of the HumanoidRootPart of my "dead body". Every time I reset, the HRP references the previous body's HRP.
I could probably just put the variable inside the inputbegan function, but I just wanted to know what was happening, why, and any more possible solutions.
local player = game:GetService('Players').LocalPlayer; local character = player.Character or player.CharacterAdded:wait(); local hrp = character:FindFirstChild('HumanoidRootPart') or character:WaitForChild('HumanoidRootPart'); local mouse = player:GetMouse(); local uis = game:GetService('UserInputService'); local runs = game:GetService('RunService'); local waterball = script:FindFirstChild('Waterball') or script:WaitForChild('Waterball'); local function quadraticBezier(t, p0, p1, p2) return (1 - t)^2 * p0 + 2 * (1 - t) * t * p1 + t^2 * p2 end local debounce = false; local size = 32; uis.InputBegan:Connect(function(key, proc) if proc then return end; if debounce then return end; if key.KeyCode == Enum.KeyCode.R then debounce = true; local pos = mouse.Hit; local curcf = CFrame.new(hrp.Position, pos.Position); local waterballA = waterball:clone(); waterballA:PivotTo(curcf); waterballA.Parent = workspace; local waterballB = waterball:clone(); waterballB:PivotTo(curcf); waterballB.Parent = workspace; local waterballC = waterball:clone(); waterballC:PivotTo(curcf); waterballC.Parent = workspace; local delta = 0; local midA = curcf:Lerp(pos, .5) * CFrame.new(0, size, 0); local midB = curcf:Lerp(pos, .5) * CFrame.new(size, 0, 0); local midC = curcf:Lerp(pos, .5) * CFrame.new(-size, 0, 0); local conc; conc = runs.Heartbeat:Connect(function() delta = delta + runs.Heartbeat:wait(); if delta > 1 then delta = 1 end waterballA:PivotTo(CFrame.new(quadraticBezier(delta, curcf.Position, midA.Position, pos.Position), pos.Position)); waterballB:PivotTo(CFrame.new(quadraticBezier(delta, curcf.Position, midB.Position, pos.Position), pos.Position)); waterballC:PivotTo(CFrame.new(quadraticBezier(delta, curcf.Position, midC.Position, pos.Position), pos.Position)); if delta == 1 then local expl = Instance.new('Explosion'); expl.Position = pos.Position; expl.Parent = workspace; waterballA:Destroy(); waterballB:Destroy(); waterballC:Destroy(); conc:disconnect() end end) task.wait(1); debounce = false; end end)
Its because you don't update the character variable when the character respawns. I'm pretty sure thats what characteradded:wait() supposedly does, as I was told by multiple people in the past, but doesn't seem to work for me whenever I try. Can't really give you an explanation on that as I don't know too much myself, but a possible solution would be to check when the character respawns and update it.
player.CharacterAdded:Connect(function(newCharacter) character = newCharacter end)