This may seem kinda stupid, but I don't know how to start a function. Here's my script so far:
local Players= game.Players.LocalPlayer function onTouched(bump) --insert the thing i need here script.ScreenGui:Clone().Parent=Players.StarterGui end
I don't know how to start the function. Could anyone help?
By "start the function," I assume you mean call the function.
To call the function, simply put the name of the function in the code. In your case, you would do onTouched(bump)
. However, in your case, it looks like you're trying to make something that clones a GUI into a player's PlayerGui when they touch a brick. Let me just re-write your code to save you the hassle.
script.Parent.Touched:connect(function(part) --this is just a one-line way of listening for an event, events can call functions, I'll give more info on that local player = game.Players:FindFirstChild(part.Parent.Name) --getting the player from their character, you can also do game.Players:GetPlayerFromCharacter(part.Parent.Name) if player then --checking if "player" exists, we have to do this in case an NPC or stray brick touches the part script:FindFirstChild("ScreenGui"):clone().Parent = player:FindFirstChild("PlayerGui") --clones the GUI and puts it in the player's PlayerGui, PlayerGui stores all the player's GUIs end end)
Functions can be called by events. You can do this multiple ways:
object.Event:connect(function(variable1, variable2) object.Event:connect(functionName(variable))
If you need clarification or more help, please comment! Otherwise, please upvote and accept!
You have the right idea, but, there are a few problems with your code;
You are not using the Variable bump
within the Function anywhere in the chunk.
You are not calling the function anywhere in the code; There isn't an event line.
You are attempting to revert the Cloned ScreenGui
's to a Child that is not, or, that may be not existant.
Let me explain functions;
Functions are a type of code in the .lua
language where you are able to run a chunk of code multiple times [May not have explained it that well. :c ], get the result of something, or to perform some special task that you are not able to do with a normal chunk, or, as I should say, perform a specific task; For example, how Anti-Viruses work [ROBLOX Anti Viruses]; Anti-Viruses call the function
on a Child
, then will call a For to loop through the currently Children within the Parent, but, however, while it is looping, the loop calls the function on each Child
that is being looped through after checking if the object is clean
, or not.
I know, my explanations are really bad. ;c
From the looks of your code, your trying to make it clone something into a Player's PlayerGui; The PlayerGui
Child of the Player
replicates, or, to dumb it down a notch, display GUIs, such as ScreenGui
's, TextLabel
's, TextButton
's, and TextBox
's to the specific Player
, but, sadly, there is no such thing as a StarterGui
Child within the Player
, I think you might have mistaken-ed the StarterGui
Service as the Player
's PlayerGui
, a common mistake I see here a lot. :P
According to the Roblox WIKI; The StarterGui Service only Clones, then reverts the Cloned Children into the Player's PlayerGui
.
Lastly, what is the Point of line 1?; You are trying to create a function where you have to Touch a specific BasePart to fire the Event/Function, but, yet, you have set up the Function to Clone
the ScreenGui
, then to revert the Cloned ScreenGui's Parent property to the Player's PlayerGui
? Unless you meant to call it later on, perhaps?
You may be wondering currently, What does he mean by calling a function?
Well, just like how I said it. To call a function, you call the function by it's function Name
. To simply call it's name, just type as next; func()
. Now, let me show you an example of what I mean;
function func() --Here is our function; We are going to call it soon return "I'm a string!" --Now, when we call this function, the 'return' keyword will return this 'string' end --This ends the chunk local func = func() --Here, we have called the function, and variable 'func' has now saved 'I'm a string!' because the function 'returned' the string print(func) --And, as a result, 'I'm a string!' will print into the Output
The impression I am getting from your code, you are trying to do here, but, you have made a few mistakes in your function. Now that we have all this done, let's rewrite the code! And sorry that we have to rewrite it, it may get confusing with the code in the future as it currently stands;
local Player = game.Players.LocalPlayer --Here is the 'LocalPlayer'; Variable 'Player' is now identifying itself as the 'Player'; The 'LocalScript''s current Client it is supporting function GUI() --Here is our new function; This will be called in a little bit local Gui = script:FindFirstChild("ScreenGui") --Variable 'Gui' is now specifying Child instance 'ScreenGui' within the 'script', will return 'nil' if the Child is not existant if Gui then --This will check to see if 'Gui', or, Instance-type 'ScreenGui', is existant, if it is existant, or, if the 'if' statement accepts it as 'true', it will run the next chunk, but, if it is nil, or 'false', it will not run the next chunk Gui = Gui:Clone() --We are now overwriting the Variable of 'Gui'; It is now identifying the Cloned 'ScreenGui' Gui.Parent = Player.PlayerGui --This will revert the Cloned 'ScreenGui''s Parent to the Player's PlayerGui end --This ends the chunk of code for the 'if' statement end --This ends the chunk of code for the function GUI() --Now, we have called it; Now that it is called, it will perform it's specific task
Sorry if my Answer/Explanations wasn't understandable. :( Hope this helped!