Raycast doesnt run after player dies

So I made this tool that makes a ray to detect which block the player is standing on then copies the block’s color and material to the tool handle but if the player dies the ray doesn’t run. What can I do? This is the script:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRP = Character:WaitForChild("HumanoidRootPart")
local LiquidV = script.Parent.Handle:FindFirstChild("HasLiquid")

script.Parent.Activated:Connect(function()
	if LiquidV.Value == false then
		LiquidV.Value = true
		local newRay = Ray.new(HumanoidRP.CFrame.p, Vector3.new(0,-1,0).unit * 10)
		local Hit = game:GetService("Workspace"):FindPartOnRay(newRay, Character)
		local HitBrickColor = Hit.BrickColor
		local HitColor3 = Hit.Color
		local HitMaterial = Hit.Material
	
	if Hit then
		script.Parent.Handle.Liquid.Transparency = 0
		script.Parent.Handle.Liquid.Color = HitColor3
		script.Parent.Handle.Liquid.Material = HitMaterial
		end
	elseif LiquidV.Value == true then
		print("Has Liquid. Play anim")
		LiquidV.Value = false
		script.Parent.Handle.Liquid.Transparency = 1
	end
	
	end)
1 Like

If the script is in starterCharacterScripts or made the child of the character another way put the script in starterPlayerScripts instead because when the player dies(character is removed) the script is destroyed.

1 Like

Well its inside a tool. Its not in startercharacterScripts

1 Like

if the tool is in the players hand at the time of death it will get destroyed, the backpack also resets when the player dies to.

basically if I use the tool then use another tool to kill the player the tool doesn’t work bur if i die while holding it it still doesnt work.

Once the player dies, the character is deleted. Your Character variable becomes invalid, because that character was deleted.

You should only get the character when the tool is activated.

Also when someone first spawns, your script may run in the Backpack before their character is parented to Workspace (or even exists)

How can I do that? with

character = Player.Character

Doesnt work.

well he has a characterAdded:Wait() do it would wait until the character is spawned

I still don’t see the other problem, what about it isnt working? but either way when the character dies the script is going to be destroyed and can’t run.

I really dont know. This is the model I made if you want to see:

https://www.roblox.com/library/6945315580/RayCast-error