Tool stops working on death

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Sorry, I am new to scripting and this is pretty much my first post here. This is my first game.

I wish for the tool to work after death.
Thank you for any help given, it is muchly appreciated

  1. What is the issue? Include screenshots / videos if possible!
    The tool stops working after death.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    GUI reset on spawn for inventory doesn’t work. Also stops the inventory system from working.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
image

--[[
Local script within tool
This tool when activated teleports the player to the clicked spot.

When equipped it clones a part which is a child of the tool to the cursor, the part turns red if the it goes out side of 
the allowed range and blue inside of the allowed range. 

When unquipped it destroys the part.
--]]

--- Services --- 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local ServerScriptService = game:GetService("ServerScriptService")

--- Folders ---
local eventsFolder = ReplicatedStorage:WaitForChild("Events")
local debrisFolder = workspace:WaitForChild("DebrisFolder")

--- Player --- 
local mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer
local skillFolder = player:WaitForChild("SkillsFolder")
local statsFolder = player:WaitForChild("PlayerStats")
local spatialMagic = skillFolder:WaitForChild("Spatial Magic")
local character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local mana = statsFolder:WaitForChild("Mana")


--- Events ---
local Event = eventsFolder:WaitForChild("Teleport")

--- Other Variables ---
local tool = script.Parent

local range = 5 + spatialMagic.Value
local equiped = false 

--- this function runs when the tool is equipped ---
local function Equip()
	local teleArea = tool:WaitForChild("TeleArea")
	local teleClone = teleArea:Clone()

	equiped = true
	teleClone.Parent = workspace.DebrisFolder
	teleClone.Name = teleArea.Name .. player.Name

	while equiped do
		task.wait()
		teleClone.Position = mouse.Hit.Position
		teleClone.Transparency = 0
		if (teleClone.Position - HumanoidRootPart.Position).Magnitude <= range then
			teleClone.Color = Color3.fromRGB(0,0,255)
		else
			teleClone.Color = Color3.fromRGB(255,0,0)
		end
	end

end

--- this function runs when the tool is unequipped ---
local function Unequip()
	equiped = false
	for i, telePart in pairs(debrisFolder:GetChildren()) do
		if telePart.Name == "TeleArea" .. player.Name then
			telePart:Destroy()
		end
	end
end

--- this functions runs when the tool is activated ---
local function OnActivate()
	if equiped and (mouse.Hit.Position - HumanoidRootPart.Position).Magnitude <= range then
		Event:FireServer(mouse.Hit.Position)
	end
end

--- connections ---
tool.Equipped:Connect(Equip)
tool.Unequipped:Connect(Unequip)
tool.Activated:Connect(OnActivate)
--[[
This script is located in Server Script Service.

It listens for the remote event sent from the teleport tool
--]]

--- Services ---
local ServerScriptService = game:GetService("ServerScriptService")
local replicatedStorage = game:GetService("ReplicatedStorage")

--- Folders ---
local eventsFolder = replicatedStorage:WaitForChild("Events")

--- Events ---
local event = eventsFolder:WaitForChild("Teleport")
local PopUpEvent = eventsFolder:WaitForChild("PopUpText")

--- Variables ---
local skillModule = require(ServerScriptService.SpellHandler.Modules.SkillModule)

-- This function runs when the remote event is sent
local function teleport(player, mouse)
	--- player
	local character = player.character
	local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
	local statsFolder = player:WaitForChild("PlayerStats")
	local mana = statsFolder:WaitForChild("Mana")
	local SkillsFolder = player:WaitForChild("SkillsFolder")
	local totalSkill = SkillsFolder:WaitForChild("Skills Total")
	local maxSkill = SkillsFolder:WaitForChild("Max Skills Allowed")
	local spatialMagic = SkillsFolder:WaitForChild("Spatial Magic")

	--- variables
	local manaCost = (mouse - HumanoidRootPart.Position).Magnitude

	if mana.Value < manaCost then
		PopUpEvent:FireClient(player, "NOT ENOUGH MANA!")  -- fire event if insufficiecnt mana
		return end
	if mana.Value >= manaCost then
		mana.Value -= manaCost
		character:MoveTo(mouse)
		skillModule.skillInc(totalSkill, maxSkill, spatialMagic) -- skill increase

	end
end

--- Conections ---
event.OnServerEvent:Connect(teleport)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

When the player dies and then respawns is it the same tool or a new tool? If the tool persists after death then Player.Character would be different and the character variable would be wrong. Also, are you getting any errors?

No, errors.
I believe the tool is persistent, but I’m not sure. Sorry new to scripting.
How would I re-new the character variable after death if that is the case?

Update Character variable everytime CharacterAdded fires.

player.CharacterAdded:Connect(function(char)
	character = char
end)

Adding this to the local script doesn’t fix the problem :frowning: .

I tested your scripts, the tool is placed in StarterPack right?

The issue is not only the Character connection, its the range of the teleport value too. Thats why you see the part on red once you died, its keeping the “old” coordinates too.

Probably you could reconnect the character, coordinates and all values, everytime the player equips the tool, or when the tool spawns in players inventory.

On my test works fine, I mean, the character is getting teleported once dead and respawn, but the max range of the teleport not, when I walked close to the point where I died, the TeleArea part turns blue again, thats what I mean about you getting the red part instead of the blue. Try walking around once you died, and you will get the blue part again.

Little detail, I disabled the module script named skillModule.skillInc, cause I dont know how it looks like, I dont know if that made a difference

1 Like

The tool is in StartPack for the time being, it won’t be there forever.

yes, it seems the part turns blue around the death area! It looks like I’ll have to reconnect the values as you said. Any idea how to do that?

Skill module just adds a certain amount of skill to the player based on the how high the skill is.

local SkillModule = {}

function SkillModule.skillCount(skill)
	print(skill.Value)
end

function SkillModule.skillInc(total, allowed, skill)
	if total.Value < allowed.Value then
		if skill.Value < 100 then
			if skill.Value < 10 then
				skill.Value += 0.5
			elseif skill.Value < 20 then
				skill.Value += 0.4
			elseif skill.Value < 30 then
				skill.Value += 0.3
			elseif skill.Value < 40  then
				skill.Value += 0.2
			elseif skill.Value < 50 then
				skill.Value += 0.1
			elseif skill.Value < 60 then
				skill.Value += 0.05
			elseif skill.Value < 70 then
				skill.Value += 0.03
			elseif skill.Value < 80 then
				skill.Value += 0.02
			elseif skill.Value < 90 then
				skill.Value += 0.01
			end
		end
	end
end

return SkillModule

I would do it by refreshing Character and HumanoidRootPart variables when the tool is equipped, using that event and its function you already have.
With these little changes, works pretty well for me:

local character
local HumanoidRootPart

--- this function runs when the tool is equipped ---
local function Equip()
	warn("equiped")
	
	-- When tool is equipped we get these variables again
	character = player.Character or player.CharacterAdded:Wait()
	HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
	
	local teleArea = tool:WaitForChild("TeleArea")
	local teleClone = teleArea:Clone()

	equiped = true
	teleClone.Parent = workspace.DebrisFolder
	teleClone.Name = teleArea.Name .. player.Name

	while equiped do
		task.wait()
		teleClone.Position = mouse.Hit.Position
		teleClone.Transparency = 0
		if (teleClone.Position - HumanoidRootPart.Position).Magnitude <= range then
			teleClone.Color = Color3.fromRGB(0,0,255)
		else
			teleClone.Color = Color3.fromRGB(255,0,0)
		end
	end
end
3 Likes

Yes, these small changes fixed the problem! Thank you :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.