I have now spent a very long time writing a script that says that if you equip this tool then in the StarterGui the visibility of an object named there in the script is set to true or false:
if script.Parent.Equipped:Connect() then
game.StarterGui.Objectives.Frame.Framel.Objective1.Visible = false
game.StarterGui.Objectives.Frame.Framel.Objective2.Visible = true
end
(This is a localscript in the tool)
But that never works there comes then always something with a nil value and there I wanted to ask if someone has a better script or a better idea to fix this problem.
First off, to access StarterGui you’d need local sgui = game:GetService("StarterGui") somewhere and change that.
Second, if you want a change to happen to a player’s current GUI, then you’d use their PlayerGui instead, so the previous part wouldn’t really matter.
local plr = game:GetService("Players").LocalPlayer
local gui = plr.PlayerGui
Third, you’re using :Connect() wrong, the correct use would be like this:
script.Parent.Equipped:Connect(function()
--script goes here
end)
After applying these fixes, your script should probably look like this:
local plr = game:GetService("Players").LocalPlayer
local gui = plr.PlayerGui
script.Parent.Equipped:Connect(function()
gui.Objectives.Frame.Frame1.Objective1.Visible = true
gui.Objectives.Frame.Frame1.Objective2.Visible = true
end)
Edit: Noticed what you were trying to do and changed my answer. Sorry if this caused any confusion.
if then statements look if something returns a true or not a nil, it proceeds with the code, unless it has an elseif, if it does, it checks through all of them as well.
Equipped:Connect() does not return anything at all, it connects a specific function which will run the code every time a tool was equipped.
Not much information was provided, but if you’re trying to see if a tool is equipped, you could do
if script:IsDescendantOf(workspace) then
game.StarterGui.Objectives.Frame.Framel.Objective1.Visible = false
game.StarterGui.Objectives.Frame.Framel.Objective2.Visible = true
end
But if you wanna run the code when the player equips the tool, you can do this: