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

My Local Script stops working after death. can someone help?

This script is placed inside StartGui and is the child of a ScreenGui. Works great until the first death. Can anyone please help me? It would be greatly appreciated!!!



-- Services

local RunService = game:GetService("RunService")

local UIS = game:GetService("UserInputService")

local Players = game:GetService("Players")

local button = script.Parent





--Variables

local DetectedNPC = nil

local Detected = false

local Chatting = false

local Skip = false

local Exit = false





-- Player

local Player = game.Players.LocalPlayer

repeat wait(0.5) until Player.Character

local Camera = game.Workspace.CurrentCamera

local Gui = script.Parent.Parent

local Sounds = script.Parent.Parent.Sounds

local PromptLable = script.Parent

local LineLable = script.Parent.Parent.LineLable







-- Character

local Character = Player.Character

local CharHMR = Character:WaitForChild("HumanoidRootPart")

local Humanoid = Character:WaitForChild("Humanoid")






-- NPC

local NPCS = game.Workspace:WaitForChild("NPCS")




-- Functions

function helpme()
		
		if Chatting == true then
			Skip = true
			Sounds.chatstart:Play()
		end
	end


button.MouseButton1Click:Connect(helpme)


	 function helpme()
		
		   
		if Detected == true then
			local Lines = DetectedNPC:FindFirstChild("Lines")
			
			if Lines then 
				Sounds.chatstart:Play()
				
				Chatting = true
				Detected = false
				
				LineLable.Text = " "
				
				PromptLable:TweenSize(UDim2.new(0, 60, 0, 60), "Out", "Linear", 0.2)
				LineLable:TweenPosition(UDim2.new(0.012, 0, 0.73, 0), "In", "Linear", 0.2)
				PromptLable:TweenPosition(UDim2.new(0, 0, 0.6, 0), "In", "Linear", 0.2)
				LineLable:TweenSize(UDim2.new(0.977, 0, 0, 100), "In", "Linear", 0.2)
				wait(0.5)
				
				for i, Line in pairs(Lines:GetChildren()) do
					local Text = Line.Value
					
					for i = 1, #Text do
						LineLable.Text = string.sub(Text, 1, i)
						Sounds.talk:Play()
						if Skip == true then
							Skip = false
							LineLable.Text = Text
							break
							
						end
						if Exit == true then
							break
						end
						wait(0.07)
					end
					if Exit == true then
						Exit = false
						break
					end
					repeat wait() until Skip == true or Exit == true
					Skip = false
					
				
				end 
				
				Exit = false
				Skip = false
				
				PromptLable:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.2)
				LineLable:TweenPosition(UDim2.new(0, 0, 1.2, 0), "Out", "Linear", 0.2)
				Sounds.chatend:Play()
				wait(0.5)
				
				Chatting = false
				Detected = false
						end
					end
		end
		
		
		button.MouseButton1Click:Connect(helpme)
			



-- Main Loop

RunService.RenderStepped:Connect(function()
	
	if Detected == false and Chatting == false then
		for i, NPC in pairs(NPCS:GetChildren()) do
			local Humanoid = NPC:FindFirstChild("Humanoid")
			local HMR = NPC:FindFirstChild("HumanoidRootPart")
			
			if Humanoid and HMR then
				if (HMR.Position - CharHMR.Position).magnitude < 15 then
					Detected = true
					DetectedNPC = NPC
					PromptLable:TweenSize(UDim2.new(0, 60, 0, 60), "In", "Linear", 0.2)
					print(DetectedNPC.Name)
						
				end
			end
		end
	end

		if Detected == true and Chatting == false then
			local Humanoid = DetectedNPC:FindFirstChild("Humanoid")
			local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
			
			if Humanoid and HMR then
				if (HMR.Position - CharHMR.Position).magnitude > 15 then
					Detected = false
					DetectedNPC = nil
					PromptLable:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Linear", 0.2)
					print("No Longer Detected NPC")
				else
					local WTSP = Camera:WorldToScreenPoint(HMR.Position)
					PromptLable.Position = UDim2.new(0, WTSP.X, 0, WTSP.Y)
					
				end
			end
		end
	

if Chatting == true then
	local Humanoid = DetectedNPC:FindFirstChild("Humanoid")
	local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
	
	if Humanoid and HMR then
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CFrame = Camera.CFrame:Lerp(HMR.CFrame *CFrame.new(-4,0,-7)*CFrame.fromOrientation(math.rad(-20), math.rad(215), 0), 0.07)

		end

else
	Camera.CameraType = Enum.CameraType.Custom
	
end 

end)


5 comments

0

any error on the log?

0

Tell us what the script suppose to do, what is not working, how it does not work, dont just give us 50 uncommented lines of code

0

Make sure all the code is in the code block.

0

I apologize for the terrible post guys. This is my first attempt at reaching out for help. I put everything in code block and I will explain what the script does. This is a Dialog script. When you get close to an NPC it detects you and tweens in a chat bubble to click. After clicking it starts the dialogue until end. After you walk away it tweens the chat bubble off screen.

0

This works perfectly until death, then the script just stops working. Ive check and unchecked respawn on death. Tried all different kinds of code. HappyTimIsHim i also tried his code and it didnt fix.

2 answers

✓ Accepted answer

This is an easy fix all you need to do is change the character = player.Character

Player.CharacterAdded:Connect(function(char)--activates when the character gets added
	Character = char  --makes global character equal to the new character
end)

Just add this line of code to the end or the beginning of the script.

10 comments

0

This didnt work :(

0

i think i know why i edited it

0

i Already tried this

0

I always consider case sensitive in scripts. What confuses me about your post is that this is a function, so adding it to the end of the script wouldnt really do anything right?

0

it is a function so it should fire when the character gets added

0

Can you implement this in my script above and post it. I've tried every way i know how and nothing changes.

0

maybe the toggle the property ResetOnSpawn if it's off?

0

checked that as well.

0

After hours of testing i can get the script to work twice in a row by using the repeat wait until Player.Character

0

i honestly don't know

So after a million trial and errors I finally was able to get a working line of code from an off the wall script. My problem is resolved and i want to thank each and every one of you for trying to help. HappyTim i'll give you some answer rep.

local Character = Player.Character or Player.CharacterAdded:Wait()
local CharHMR = Character:WaitForChild("HumanoidRootPart")


function InitializeCharacter(character)
	    -- Update the Torso and Character variable
	    -- so that the camera locks on to the player's current Torso.
	    Character = character
CharHMR = character:WaitForChild("HumanoidRootPart")

end

Player.CharacterAdded:Connect(InitializeCharacter)
	-- Initialize the Character we have right now.
	InitializeCharacter(Character)

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