So here's a script I was working on.
function onTouch() print("Touched") local Player = game.Players.LocalPlayer --Detects the local player local GUI = script.Parent:Clone() -- Clones the GUI (which is the parent of the script) GUI.Parent = Player.PlayerGui -- is meant to put the GUI into the player Gui print("Done") end script.Parent.Parent.Touched:connect(onTouch) -- the GUI is located in a part
So I got to test it on a script... and it doesn't work. It doesn't get past the first print function so I'm wondering what I'm doing wrong? Can someone explain? Thanks.
The problem with this script is that you are attempting to do two things at once. Not only do you want your script to be a LocalScript (so you can use game.Players.LocalPlayer) but you also seem to want to have it as the child of a brick. The problem is that you will need to have this as a Script, saying something along these lines (pardon my anonymous function):
script.Parent.Touched:connect(function(part) if not part.Parent:FindFirstChild("Humanoid") then return end for _,v in pairs(game.Players:GetChildren()) do if v.Character == part.Parent then -- Limb of character? if v:FindFirstChild("PlayerGui") and not v.PlayerGui:FindFirstChild("GUI") then -- Player doesn't already have a GUI? local gui = script.Parent.GUI:Clone() gui.Parent = v.PlayerGui end break end end end)
Here is what your userdata in Workspace should look like if you choose to use the method above.
The reason your script didn't run is because LocalScripts do not run unless they are the descendant of an active player's character model, the Backpack of the player, or the PlayerGui.
With this method, you can still run a localscript, but this way it will actually work since once it becomes the descendant of a PlayerGui, it will activate for that specific player. Hope this helped, and if it did don't forget to upvote!