this code is inside a gui, and it worked perfectly, but now it just dosent for some reason without change. it says the the part name is a nil value despite it being in workspace, and I know I named it right, I literally copy and pasted the name straight from the part to put into this script. heres the script: (just to clarify, this is only happening with the 3rd line, aka the part.touched thing.)
local part = game.workspace:FindFirstChild("Beggining") part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then script.Parent.Parent.Enabled= true wait(5) script.Parent.Parent.Enabled = false end end) local part = workspace:FindFirstChild("Why") part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then wait(1) script.Parent.Parent.Enabled= true wait(5) script.Parent.Parent.Enabled = false end end) local part = workspace:FindFirstChild("YES") part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then wait(1) script.Parent.Text.Text = "TEST" script.Parent.Text2.Text = "TEXT" script.Parent.Parent.Enabled= true wait(5) script.Parent.Parent.Enabled = false end end)
Improvements
Use a Local Function since the change between the events is practically the same
Use WaitForChild()
to make sure an instance exists before using it
Use Local Variables for instances which you want to call multiple times throughout the script
Issues
You're using multiple local variables with the same name, causing them to overwrite each other, instead, name them differently.
If you use FindFirstChild()
you should also be checking that the returned value is not nil
Revised LocalScript
local frame = script.Parent local gui = frame.Parent local part1 = workspace:WaitForChild("Beggining") local part2 = workspace:WaitForChild("Why") local part3 = workspace:WaitForChild("YES") local function Enabler(hit, check) if hit and hit.Parent and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then if check == 2 or check == 3 then wait(1) if check == 3 then frame:WaitForChild("Text").Text = "TEST" frame:WaitForChild("Text2").Text = "TEXT" end end gui.Enabled= true wait(5) gui.Enabled = false end end part1.Touched:Connect(function(hit) Enabler(hit, 1) end) part2.Touched:Connect(function(hit) Enabler(hit, 2) end) part3.Touched:Connect(function(hit) Enabler(hit, 3) end)