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
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?
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.
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).
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.
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