Nothing seems to exist in playergui after I respawn

I am currently making a laser gun. Everything works perfectly but as soon as I respawn, to the local script in my tool, it thinks that theres no ui in the playergui. I confirmed this by checking the playergui from the explorer tab and everything was still there. but in my local script its infinitely yielding when I am doing any actions such as shooting or sometimes nil if I dont actually specify it in a variable. I don’t know what is causing this. also any bool value I have in the startergui is not replicating from the local script when ever the value is changed, but to the local script it thinks it changed. I am totally confused, any help to the right direction would help. Thanks.

1 Like

I think what’s happening is that the local script is referencing an old UI after you respawn somehow. Where is your local script placed?

Could you send us the script?

my local script is parented to the tool used, im not sure how its referencing old ui if tools get remade on respawn

its a long local script, im not quite sure what part would help here, what do you need?

I just want to see the script, I’ll go through it dw, and send a screenshot of the whole explorer

I had a problem like this but with animations. Instead of defining your UI outside your functions define them inside. This way if you have any connections it will define a new variable each time.

yeah I tested that for the shooting aspect but it was still infinitely wielding

UI’s get reset when a player spawns, maybe if you check off screengui – > reset on death

local UIS  = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local LocalPlayer = game.Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local AmmoFolder = PlayerGui:WaitForChild("Interface"):WaitForChild("Ammo"):WaitForChild("Frame")
local Ammo = AmmoFolder:WaitForChild("Ammo")
local MagAmmo =  AmmoFolder:WaitForChild("MagAmmo")
local Crosshair = PlayerGui:WaitForChild("GunGUI"):WaitForChild("Crosshair")
local Horizontal = Crosshair:WaitForChild("Horizontal")
local Mouselock = PlayerGui:WaitForChild("Mouselock"):WaitForChild("Lock")
local Mouse = LocalPlayer:GetMouse()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:wait()
while not Character or not Character.Parent do
	Character = LocalPlayer.Character
	wait()
end
Mouse.TargetFilter = workspace:WaitForChild("Bin")
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local Tool = script.Parent
local data = require(Tool:WaitForChild("data"))
local Animation = Tool:WaitForChild("Anim")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("Guns")
local ReloadRemote = Remotes:WaitForChild("Reload")
local FireRemote = Remotes:WaitForChild("Fire")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local GunHandler = require(Modules:WaitForChild("GunHandler"))
local TweenService = game:GetService("TweenService")
local Weapon
local Hole
local GunSpread = 0
local Aiming = false
local Shooting = false
local isReloading = false
local Held = false
local Enabled = false

local IdleTrack = Humanoid:LoadAnimation(Animation:WaitForChild("Idle"))
IdleTrack.Priority  = Enum.AnimationPriority.Action
local ShootTrack = Humanoid:LoadAnimation(Animation:WaitForChild("Shoot"))
ShootTrack.Priority  = Enum.AnimationPriority.Action
local ReloadTrack = Humanoid:LoadAnimation(Animation:WaitForChild("Reload"))
ReloadTrack.Priority  = Enum.AnimationPriority.Action

function FadeAmmo()
	if Enabled then
		for i,v in pairs(AmmoFolder:GetChildren()) do
			if v:IsA("Frame") then
				local Tween = TweenService:Create(v,TweenInfo.new(.25,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{BackgroundTransparency = 0})
				Tween:Play()
			elseif v:IsA("TextLabel") then
				local Tween = TweenService:Create(v,TweenInfo.new(.25,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{TextTransparency = 0})
				Tween:Play()
			end
		end
	else
		for i,v in pairs(AmmoFolder:GetChildren()) do
			if v:IsA("Frame") then
				local Tween = TweenService:Create(v,TweenInfo.new(.25,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{BackgroundTransparency = 1})
				Tween:Play()
			elseif v:IsA("TextLabel") then
				local Tween = TweenService:Create(v,TweenInfo.new(.25,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{TextTransparency = 1})
				Tween:Play()
			end
		end
	end
end

function FadeCross()
	if Enabled then
		for i,v in pairs(Crosshair:GetDescendants()) do
			if v:IsA("Frame") and v.Name ~= "Vertical" and v.Name ~= "Horizontal" then
				local Tween = TweenService:Create(v,TweenInfo.new(.25,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{BackgroundTransparency = 0})
				Tween:Play()
			end
		end
	else
		for i,v in pairs(Crosshair:GetDescendants()) do
			if v:IsA("Frame") and v.Name ~= "Vertical" and v.Name ~= "Horizontal" then
				local Tween = TweenService:Create(v,TweenInfo.new(.25,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{BackgroundTransparency = 1})
				Tween:Play()
			end
		end
	end
end

function OnUpdate()
	local LastTick = tick()
	local c = coroutine.wrap(function()
		while Enabled == true do
			if isReloading == false then
				Ammo.Text = data.ammo
				MagAmmo.Text = data.magammo*data.maxammo
			else
				Ammo.Text = 0
			end
			if tick() - LastTick >= data.bulletspreaddroprate then
				GunSpread = math.clamp(GunSpread - data.bulletspreaddrop,0,data.maxbulletinfluence)
				LastTick = tick()
			end
			if Aiming == false then
				for i,v in pairs(Crosshair:GetDescendants()) do
					if v.Name == "Center" then
						v.Visible = false
					end
					if v.Name == "Left" then
						local Tween = TweenService:Create(v,TweenInfo.new(data.bulletspreaddroprate,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{Position = UDim2.new(-math.clamp(math.floor(Root.Velocity.Magnitude)+GunSpread,data.minspread,data.maxspread)/2,0,.5,0), Size = UDim2.new(10,0,.45,0)})
						Tween:Play()
					elseif v.Name == "Right" then
						local Tween = TweenService:Create(v,TweenInfo.new(data.bulletspreaddroprate,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{Position = UDim2.new(math.clamp(math.floor(Root.Velocity.Magnitude)+GunSpread,data.minspread,data.maxspread)/2,0,.5,0), Size = UDim2.new(10,0,.45,0)})
						Tween:Play()
					end
				end
			else
				for i,v in pairs(Crosshair:GetDescendants()) do
					if v.Name == "Center" then
						v.Visible = true
					end
					if v.Name == "Left" then
						local Tween = TweenService:Create(v,TweenInfo.new(data.bulletspreaddroprate,Enum.EasingStyle.Exponential,Enum.EasingDirection.Out,0,false,0),{Position = UDim2.new(0,0,.5,0), Size = UDim2.new(0,0,.45,0)})
						Tween:Play()
					elseif v.Name == "Right" then
						local Tween = TweenService:Create(v,TweenInfo.new(data.bulletspreaddroprate,Enum.EasingStyle.Exponential,Enum.EasingDirection.Out,0,false,0),{Position = UDim2.new(0,0,.5,0), Size = UDim2.new(0,0,.45,0)})
						Tween:Play()
					end
				end
			end
			RunService.Heartbeat:wait()
		end
	end)
	c()
end

function Aim()
	local Camera = workspace.CurrentCamera
	local Tween = TweenService:Create(Camera,TweenInfo.new(data.bulletspreaddroprate,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 70 - 30})
	Tween:Play()
	repeat RunService.Heartbeat:wait() until Aiming == false
	local Tween = TweenService:Create(Camera,TweenInfo.new(data.bulletspreaddroprate,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 70})
	Tween:Play()
end

function ActivateProcesses()
	Hole = Weapon.Hole
	IdleTrack:Play(.05)
	Enabled = true
	OnUpdate()
	FadeAmmo()
	FadeCross()
	print(Mouselock.Value)
	Mouselock.Value = true
	print(Mouselock.Value)
	UIS.MouseIconEnabled = false
	ContextActionService:BindAction("Reload", Reload, true, Enum.KeyCode.R)
end

function Equipped()
	repeat 
		for i,v in pairs(Character:GetChildren()) do
			if v:IsA("Model") then
				if v.Name == Tool.Name then
					Weapon = v
				end
			end
		end
		RunService.Heartbeat:wait()
	 until Weapon
	ActivateProcesses()
end

function Uneuipped()
	Weapon = nil
	Hole = nil
	Enabled = false
	FadeCross()
	FadeAmmo()
	IdleTrack:Stop()
	ReloadTrack:Stop()
	ShootTrack:Stop()
	Mouselock.Value = false
	UIS.MouseIconEnabled = true
	Held = false
	Aiming = false
	ContextActionService:UnbindAction("Reload")
end

function Shoot()
	local c = coroutine.wrap(function()
	local DesiredTime = data.firerate
	local StartTime = tick()
	if Shooting == false then
			Shooting = true
			for i,v in pairs(Horizontal:GetChildren()) do
					print(v.Name)
			end
			print(2)
			while Enabled and Weapon and Hole and data.ammo > 0 and Held == true and isReloading == false do
				
			local obj = Horizontal.Left
			local Spread = math.abs(obj.Position.X.Scale)
			local Range = 8	
			local Pos = Mouse.Hit.p
			local Dir = CFrame.new(Hole.CFrame.p,Pos)*CFrame.Angles(math.rad(math.random(-Spread,Spread)/Range),math.rad(math.random(-Spread,Spread)/Range),0)
			local GunData = {
					Character = Character,
					Start = Hole.CFrame.p,
					End = Dir.lookVector,
					Hole = Hole,
					Neon = data.neon,
					equipsound = data.equipsound,
					shootsound = data.shootsound,
					reloadsound = data.reloadsound,	
			}
			GunHandler:CreateRay(GunData)
			ShootTrack:Play()
			data.ammo = data.ammo - 1
			if Aiming == false then
				GunSpread = math.clamp(GunSpread + data.bulletspread,0,data.maxbulletinfluence)
			end
			if data.auto == false or isReloading == true then
				Held = false
			end
			repeat
			RunService.Heartbeat:wait()
			until tick() - StartTime > DesiredTime
			StartTime = tick()
			if Held == false then 
				Shooting = false
				break 
			end
		end
		Held = false
		Shooting = false	
	end
	end)
	c()
end

function Reloading()
	if Enabled == true and isReloading == false and data.ammo < data.maxammo and Hole then
		local GunData = {
			Hole = Hole,
			reloadsound = data.reloadsound
		}
		isReloading = true
		local Recieved = false
		data.ammo = 0
		ReloadTrack:Play()
		GunHandler:Sound(GunData,"reload",.2)
		local c = coroutine.wrap(function()
		wait(ReloadTrack.Length)
			repeat RunService.Heartbeat:wait() until Recieved == true
			print("gppd")
			isReloading = false
		end)
		c()
		if ReloadRemote:InvokeServer(Tool.Name) == true then
			print("doneeeeeee")
			if data.magammo > 0 then
				data.magammo = data.magammo - 1
				data.ammo = data.maxammo
				Recieved = true
				return true
			end
		end
	end
end

Mouse.Button1Down:Connect(function()
	Held = true
	Shoot()
end)

Mouse.Button1Up:Connect(function()
	Held = false
end)

Mouse.Button2Down:Connect(function()
	if Enabled == true then
		Aiming = true
		Aim()
	end
end)

Mouse.Button2Up:Connect(function()
	Aiming = false
end)

function Reload(ActionName,InputState,InputObject)
	if InputState == Enum.UserInputState.Begin then
		Reloading()
	end
	return Enum.ContextActionResult.Pass
end

Tool.Equipped:Connect(Equipped)
Too> Blockquotel.Unequipped:Connect(Uneuipped)

This is the local script, its connected to events and server scripts aswell

For example, when I had a problem similar to this my tool was connected to a .Equipped function. It worked the first time I spawned, but after I died it failed to load the animation since it was referencing an old humanoid. To fix this, I just defined the humanoid inside the function and that seemed to fixed it. It’s weird because the script gets ran each time you spawn.

yeah I also had that animation issue but I solved it by the Character repeat wait thing I did at the start. I think you might be right on referencing on each function. But I will wait until I get more takes from others before I decide to accept an answer

I’m sure it’s because you’re using old variables. If the UI doesn’t exist it’s because the ones you originally set your variables to were destroyed. I would just define everything in the equipped function and send it over as a paramater to the other ones, or just define it in each function.

2 Likes

I just followed your suggestion and it worked :smiley:. I don’t understand though, I thought Playergui and Backpack automatically get deleted and renewed?. I did the referencing under every function that needs it and it works, but do you think referencing every function will cause any problems later?. Those are my two questions but thank you sir.

Yea I’m not sure about that. In my problem, the tool was in starter pack so it’s fresh each time they spawn but they were somehow still old variables. It even printed “Ran” each time at the top of the script when I died. I can’t really think of any problems besides that it might take up a bit more memory by defining more variables?

1 Like