Help with a script

Hello Developers! :wink:

I am having trouble with a script I was writing, and I have no idea why it is not working. Before you look at the script below, it is only the User Input Service and Input Began that is not working. All of the rest is just in the same script. Thank you for all of your help.

local Distance = 10
local talking = false
local players = game:GetService("Players")
local starterGui = game:GetService("StarterGui")
local screengui = starterGui:FindFirstChild("ScreenGui")
local frame = screengui.Frame
local nf = frame.NextFrame
local nb = nf.NextButton
nf.Visible = false
nb.Visible = false
frame.Position = UDim2.new(0, 0,1.2, 0)
local talkable = true
local uis = game:GetService("UserInputService")
while wait()do 
	for i,v in pairs(workspace:GetChildren()) do 
		if game.Players:FindFirstChild(v.Name) then 
			if(v.PrimaryPart.Position - script.Parent.Position).magnitude <= Distance then 
				if talking == false then
					local character = v
					local player = players:GetPlayerFromCharacter(character)
					local playerGui = player:WaitForChild("PlayerGui")
					local screenGui = playerGui:WaitForChild("ScreenGui")
					local frame = screenGui:WaitForChild("Frame")
					frame:TweenPosition(UDim2.new(0, 0,0.73, 0), "Out", "Back")
					local nextframe = frame:WaitForChild("NextFrame")
					local nextbutton = nextframe:WaitForChild("NextButton")
					local talktext = frame:FindFirstChild("TalkText")
					if talkable == true then
						uis.InputBegan:Connect(function(k)
							if k.KeyCode == Enum.KeyCode.E then
								nb.Visible = true
								nf.Visible = true
								talktext.Text = "Hello!"
								wait(0.1)
								nb.MouseButton1Click:Connect(function()
									talktext.Text = "Bye!"
								end)
							end
						end)
					end
				end
			else 
				if talking == false then
					local character = v
					local player = players:GetPlayerFromCharacter(character)
					local playerGui = player:WaitForChild("PlayerGui")
					local screenGui = playerGui:WaitForChild("ScreenGui")
					local frame = screenGui:WaitForChild("Frame")
					local nextframe = frame:WaitForChild("NextFrame")
					local nextbutton = nextframe:WaitForChild("NextButton")
					local talktext = frame:FindFirstChild("TalkText")
					frame:TweenPosition(UDim2.new(0, 0,1.2, 0), "Out", "Back")
				end
			end 
		end 
	end 
end

Merry Christmas :christmas_tree:

1 Like

Is there red error lines under the text? If so just check spelling, but if there isn’t then I wouldn’t know, but some of my scripts haven’t been working lately even though they worked a week/few days ago…might be a patch?

1 Like

This is the only bit that is not working and I can’t see any spelling error here.

uis.InputBegan:Connect(function(k)
							if k.KeyCode == Enum.KeyCode.E then
								nb.Visible = true
								nf.Visible = true
								talktext.Text = "Hello!"
								wait(0.1)
								nb.MouseButton1Click:Connect(function()
									talktext.Text = "Bye!"
								end)

You’re making connections inside of connections inside of a generic for loop which iterates through instances inside of a while true do loop which runs indefinitely.

2 Likes

Can you say that again, but uhh, in simple form.

pairs(workspace:GetChildren())

Maybe Players:GetPlayers?

local Distance = 10
local talking = false
local players = game:GetService("Players")
local player = players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local screengui = playergui.ScreenGui
local frame = screengui.Frame
local nf = frame.NextFrame
local nb = nf.NextButton
nf.Visible = false
nb.Visible = false
frame.Position = UDim2.new(0, 0,1.2, 0)
local talkable = true
local uis = game:GetService("UserInputService")
while wait()do 
	for i,v in pairs(workspace:GetChildren()) do 
		if game.Players:FindFirstChild(v.Name) then 
			if(v.PrimaryPart.Position - script.Parent.Position).magnitude <= Distance then 
				if talking == false then
					local character = v
					local player = players:GetPlayerFromCharacter(character)
					local playerGui = player:WaitForChild("PlayerGui")
					local screenGui = playerGui:WaitForChild("ScreenGui")
					local frame = screenGui:WaitForChild("Frame")
					frame:TweenPosition(UDim2.new(0, 0,0.73, 0), "Out", "Back")
					local nextframe = frame:WaitForChild("NextFrame")
					local nextbutton = nextframe:WaitForChild("NextButton")
					local talktext = frame:FindFirstChild("TalkText")
					if talkable == true then
						uis.InputBegan:Connect(function(k)
							if k.KeyCode == Enum.KeyCode.E then
								nb.Visible = true
								nf.Visible = true
								talktext.Text = "Hello!"
								wait(0.1)
								nb.MouseButton1Click:Connect(function()
									talktext.Text = "Bye!"
								end)
							end
						end)
					end
				end
			else 
				if talking == false then
					local character = v
					local player = players:GetPlayerFromCharacter(character)
					local playerGui = player:WaitForChild("PlayerGui")
					local screenGui = playerGui:WaitForChild("ScreenGui")
					local frame = screenGui:WaitForChild("Frame")
					local nextframe = frame:WaitForChild("NextFrame")
					local nextbutton = nextframe:WaitForChild("NextButton")
					local talktext = frame:FindFirstChild("TalkText")
					frame:TweenPosition(UDim2.new(0, 0,1.2, 0), "Out", "Back")
				end
			end 
		end 
	end 
end

It’s because you’re referencing StarterGui and not the player’s PlayerGui folder (which is where content from StarterGui is copied to automatically).

2 Likes

Did you change anything of the script?

Yes, I added a small comment at the bottom which indicates the changes I made.

local players = game:GetService("Players")
local player = players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local screengui = playergui.ScreenGui

playerGui.ScreenGui may need to be "playerGui:WaitForChild(“ScreenGui”) to allow for it to load first.

2 Likes

Yeah, I got an error of this so you’re probably right.
Workspace.Part.Script:5: attempt to index nil with 'WaitForChild'

@forummer what do I change after getting this error?

No,it’s just you forgot to check processed event.

I’m not exactly sure since I just started out with scripting, most advanced thing iv done is a click detect part spawner :sweat_smile:

1 Like

This is a workspace script? You can only detect user input from local scripts.

2 Likes

Can you be more precise about what is not working. Is it not setting the .Text to “Hello” or not setting it to “Bye”.

As @Forummer indicated the code is very spaghetti like doing things that are inefficient and difficult to debug. For example in this “not working” section alone you are creating a new Connection on the nb.MouseButton1Click every time E is pressed, but never disconnecting so when the button is clicked each connection is going to try setting the .Text to “Bye”

Oh… I didn’t know that. It is a workspace script inside a part. How can I detect it then because it needs it be a script and not local since you can’t to locals scripts inside a part.

local part = workspace:WaitForChild("Part") --change this reference
local uis = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local screengui = playergui.ScreenGui
local frame = screengui.Frame
local nf = frame.NextFrame
local nb = nf.NextButton
local tt = frame.TalkText

nf.Visible = false
nb.Visible = false
frame.Position = UDim2.new(0, 0, 1.2, 0)

local distance = 10
local talking = false
local talkable = true

while task.wait()do 
	for _, plr in ipairs(players:GetPlayers()) do
		local char = plr.Character
		local hrp = char:FindFirstChild("HumanoidRootPart")
		if (hrp.Position - part.Position).magnitude <= distance then 
			if not talking then
				local playerGui = plr:WaitForChild("PlayerGui")
				local screenGui = playerGui:WaitForChild("ScreenGui")
				local frame = screenGui:WaitForChild("Frame")
				local nextframe = frame:WaitForChild("NextFrame")
				local nextbutton = nextframe:WaitForChild("NextButton")
				local talktext = frame:WaitForChild("TalkText")
				frame:TweenPosition(UDim2.new(0, 0, 0.73, 0), "Out", "Back", 1)
				
				if talkable then
					uis.InputBegan:Connect(function(key, processed)
						if processed then
							return
						end
						if key.KeyCode == Enum.KeyCode.E then
							nf.Visible = true
							nb.Visible = true
							tt.Text = "Hello!"
							nb.MouseButton1Click:Connect(function()
								tt.Text = "Bye!"
							end)
						end
					end)
				end
				
			elseif talking then
				local playerGui = plr:WaitForChild("PlayerGui")
				local screenGui = playerGui:WaitForChild("ScreenGui")
				local frame = screenGui:WaitForChild("Frame")
				local nextframe = frame:WaitForChild("NextFrame")
				local nextbutton = nextframe:WaitForChild("NextButton")
				local talktext = frame:WaitForChild("TalkText")
				frame:TweenPosition(UDim2.new(0, 0, 1.2, 0), "Out", "Back", 1)
			end
		end 
	end 
end
1 Like

I think I’ve fixed it now. Thanks for the help.