Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why my instance.new script doesnt work in the starterGui?

Asked by 6 years ago

i had a script that would insert a TextLabel into my screen gui but it doesnt work heres my script: and in the output it was saying "LevelGui is not a valid member of StarterGui"

script.Parent.Touched:connect(function(hit)

 if hit ~= nil then
  local char = hit.Parent
  if char ~= nil then
   local humanoid = char:FindFirstChild("Humanoid")
   if humanoid ~= nil then
    game.StarterGui.LevelGUI = Instance.new ("TextLabel")
   end
  end
 end
end)

and "LevelGui" is a ScreenGui that i added in the StarterGui

0
Also, you received a helpful comment on your previous (duplicate) question: https://scriptinghelpers.org/questions/54840/my-instancenew-script-in-to-add-an-object-in-the-startergui-isnt-working Shawnyg 4330 — 6y

1 answer

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago

Here are the problems with the script above.

  1. connect is deprecated. Use Connect instead. This isn't causing your script to break, but it is good to know, since deprecated methods are no longer supported by roblox.

  2. You wouldn't change the starter gui, as that is what is replicated to the player, not what they see. You would have to go to the player gui.

  3. This would require remote events since a script cannot access the playergui. Otherwise, this will work in studio, but not in game.

  4. The error message occurred because of the equals sign. It thinks that LevelGui is a property of the StarterGui, but it isn't. If you are trying to insert a textlabel, then you would parent the new instance to the LevelGui.

Here is the solution to all four problems.

Put a RemoteEvent called MakeTextLabel inside the ReplicatedStorage

LocalScript inside the gui called LevelGUI

local replicated = game:GetService("ReplicatedStorage")
local createtext = replicated:WaitForChild("MakeTextLabel")

createtext.OnClientEvent:Connect(function()
    local textlabel = Instance.new("TextLabel")
    -- modify/customize the text label
    textlabel.Parent = script.Parent
    -- if you want to delete it after, put the code here, otherwise leave blank.
end)

Script inside part.


local replicated = game:GetService("ReplicatedStorage") local createtext = replicated:WaitForChild("MakeTextLabel") script.Parent.Touched:Connect(function(hit) local player = nil for i,v in pairs(game.Players:GetPlayers())do if v and v.Character and v.Character:IsAncestorOf(hit) then player = v break end end if player == nil then return end createtext:FireClient(player) end)
Ad

Answer this question