Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I reset a script after your character dies?

I'd like to know if there is a way to reset a script because I made an ability script that gives you an ability depending on which one you choose, but when you die, it only activates your previous ability. If there is a way to reset a script then I would like to know! If there is another way, here are two scripts (both of which are localscripts and I am not changing that):

local pow = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("PowerGui2"):WaitForChild("Frame")
local ability = pow.Power

local plr = game.Players.LocalPlayer.Character
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(inputObject, gameProcessed)
    if inputObject.KeyCode == Enum.KeyCode.Q and ability.Value == 1 then
            game.ReplicatedStorage.Speedparticle:Clone().Parent = game.Players.LocalPlayer.Character:FindFirstChild("UpperTorso")
            game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").WalkSpeed = 40
            wait(10)
                game.Players.LocalPlayer.Character:FindFirstChild("UpperTorso").Speedparticle:Remove()
            game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").WalkSpeed = 16
        end

local RS = game:GetService("ReplicatedStorage")

local remoteEvent = RS:WaitForChild("RemoteEvent")

UIS.InputBegan:Connect(function(inputObject, gameProcessed)
    if inputObject.KeyCode == Enum.KeyCode.Q and ability.Value ==2 then
        remoteEvent:FireServer() --Nothing passed to the server
						wait(0.5)
		for i, Child in pairs(plr:getChildren()) do
			if Child:IsA('Part') or Child:IsA('MeshPart') or Child:IsA('Accessory') then
				Child.Transparency = 0.5
			end
		end	
		plr:FindFirstChild("Head").face.Transparency = 0.5
		plr:FindFirstChild("HumanoidRootPart").Transparency = 1
		wait(9.5)
		for i, Child in pairs(plr:getChildren()) do
			if Child:IsA('Part') or Child:IsA('MeshPart') or Child:IsA('Accessory') then
				Child.Transparency = 0
			end
		end	
			plr:FindFirstChild("Head").face.Transparency = 0
		plr:FindFirstChild("HumanoidRootPart").Transparency = 1
    end
end)

UIS.InputBegan:connect(function(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.Q and ability.Value == 3 then
		game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("beserk").Value = true
		for i, tool in pairs(plr:getChildren()) do
			if tool:IsA('Tool') then
				for i, child in pairs(tool:getChildren()) do
					if child.Name == "Blade" then
						game.ReplicatedStorage.BeserkParticle:Clone().Parent = child
					elseif child.Name == "BeserkScript" then
						child.Disabled = false
					end
				end
			end
			end
			plr.Humanoid.WalkSpeed = 25
		wait(10)
		plr.Humanoid.WalkSpeed = 16
			for i, tool in pairs(plr:getChildren()) do
			if tool:IsA('Tool') then
				for i, child in pairs(tool:getChildren()) do
					if child.Name == "Blade" then
						for i, particle in pairs(child:getChildren()) do
							if particle:IsA('ParticleEmitter') then
								particle:Remove()
							end
						end
					end
				end
			end
		end
		game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("beserk").Value = false
		end
end)

local remoteEvent2 = RS.RemoteEvent2

UIS.InputBegan:connect(function(inputObject, gameProcessed)
	if inputObject.KeyCode == Enum.KeyCode.Q and ability.Value == 4 then
		remoteEvent2:FireServer()	
	end
end)
end)

The script below is the script that applies your choice

local pow = game.Players.LocalPlayer.PlayerGui.PowerGui2.Frame.Power
script.Parent.Select1.MouseButton1Click:Connect(function()
	local gui = game.Players.LocalPlayer.PlayerGui.PowerGui2.Frame
	gui.Select1.Text = "Selected"
	gui.Select2.Text = "Select"
	gui.Select3.Text = "Select"
	gui.Select4.Text = "Select"
	pow.Value = 1
end)

script.Parent.Select2.MouseButton1Click:Connect(function()
	local gui = game.Players.LocalPlayer.PlayerGui.PowerGui2.Frame
	gui.Select1.Text = "Select"
	gui.Select2.Text = "Selected"
	gui.Select3.Text = "Select"
	gui.Select4.Text = "Select"
	pow.Value = 2
end)

script.Parent.Select3.MouseButton1Click:Connect(function()
	local gui = game.Players.LocalPlayer.PlayerGui.PowerGui2.Frame
	gui.Select1.Text = "Select"
	gui.Select2.Text = "Select"
	gui.Select3.Text = "Selected"
	gui.Select4.Text = "Select"
	pow.Value = 3
end)

script.Parent.Select4.MouseButton1Click:Connect(function()
	local gui = game.Players.LocalPlayer.PlayerGui.PowerGui2.Frame
	gui.Select1.Text = "Select"
	gui.Select2.Text = "Select"
	gui.Select3.Text = "Select"
	gui.Select4.Text = "Selected"
	pow.Value = 4
end)

1 answer

✓ Accepted answer

Hello,

We can use something called Humanoid.Died to achieve this result. Humanoid.Died senses when the Player's Humanoid is dead.

Here's an example of how it would work.

plr = game:GetService("Players").LocalPlayer
plr.Character:WaitForChild("Humanoid").Died:Connect(function()
	--reset all values
end

Now, to reset your script, we can copy the script and self destruct it, here's an example when assuming that this script it in the original script that you want to replace,

plr = game:GetService("Players").LocalPlayer
plr.Character:WaitForChild("Humanoid").Died:Connect(function()
	copy = script:Clone()
	copy.Parent = script.Parent
	script:Destroy()
end

What this does is tell the script to copy itself, then tell the script to move the copy into the same Parent, and then finally delete the old script.

Hopefully this was able to clear up some confusion.

3 comments

0

I don't exactly know how to reset values though...

0

Let me further add to my answer then, thank you for clarifying :)

0

I added a wait(6) before all of that stuff happpened

This discussion is preserved as it was. New answers, comments, and votes are closed.