#"return end" ? Whaaaaat ?
1 messages · Page 1 of 1 (latest)
@green swan
Say you are wanting to use userinputservice to make a fireball
You make it "e"
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(inp, gpe)
end)
what return end means is like this.
in the inputbegan parameters for the function, it takes 2 arguments. input, which is the key pressed, and gpe, or "game-processed-event"
GPE happens when you type in the chatbox or type in a gui.
Correct.
To make sure we don't constantly throw out fireballs when we are typing in chat, we need to rule out gpe in the script
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(inp, gpe)
if gpe then return end
end)
what return does is it essentially says "stop the code here"
so if we try casting the fireball, we will notice it no longer working when we are in a gpe (typing in chat for example), but works when we are not doing a gpe.
Returning is extremely helpful if you want to stop what the code is doing.
You use returns usually in if statements and general code, and break in loops
local x = 0
repeat
x += 1
if x == somevalue then break end
until x == 10
Here's an example of break
OKay let's think about it like this.
Let's say you have an ability, but you dont want the player to be able to spam it. So you add a debounce.
local debounce = false
--uis
if debounce == true then return end
debounce = true
--code here for ability
end
What this does is: when the player presses the keybind, the script runs the userinputservice.InputBegan, and then does an if statement check to see if debounce is true, and if it is, it stops all the code. so the rest of the code doesn't continue.
Let's say we make a combat system, and we want to add a "attack stun" effect. This would be one of the lines of how you do it.
Do note that you can also write something like that as:
local debounce = false
if debounce == true then
return
end
It might be easier to tell me the type of games you want to make, just so I can make an example that might be more relevant to you 😅
Okay then: let's say you are doing a word problem in which the player drags and drops cupcakes to complete a customer's order
local CustomerTypes = {
A = {CupcakeMax = 5, Tip = 10},
B = ... --so on so forth
}
--pseudo code
if CustomerGivenCupcakes == CustomerTypes[A][CupcakeMax] then
print("Player has given enough cupcakes!")
GivePlayerTip(CustomerTypes[A][Tip]
return
else
print("Player needs to give: " .. tostring(CustomerTypes[A][CupcakeMax] - CustomerGivenCupcakes) .. " more cupcakes!")
end
--code below this would keep handling inputs for the player's interactions with the customer. A Return would be useful to stop the code from continuing to prevent players from giving too many cupcakes.
A more literal example of how it might be done
return and breaks work pretty much exactly like each other
except breaks handle loops
--do code like this
its more readable
as for your question:
If you do Math(2,3) it will print Hello, however if you do Math(4,2) it will nnot print hello.
also you missed a quotation mark on your hello
` sign is to the left of "1" on your keyboard
above tab, under esc
Here's a good example for how you might apply it
local Answer = 10
local function Question22(A, B)
if A+B ~= Answer then
print("Not the correct answer")
return
end
print("Correct! Moving on to next stage")
--moves player
end