need help detecting game lighting in my lua script - lua

I am trying to make a script, in Lua code, that detects the level of lighting in my game. I am pretty new to Lua so as far as I know lighting in the game is a number (2, 10, 5).
if game:GetService("Lighting").Brightness < 1 then
game.StarterGui.ScreenGui.output.Text = "You are getting cold";
end
I am not sure if it is just not detecting game:GetService("lighting").Brightness as a number or if the game does not know what game.StarterGui.ScreenGui.output.Text is. I've testing this multiple times, making small alteration every time, but most of the time I got no error. With the code now there is no error message.

Do not use game.StarterGui.
StarterGui is what gui is put inside a player's PlayerGui when they join. Players do NOT see StarterGui.
Instead:
game.Players.PlayerAdded:Connect(function(plr)
if game:GetService("Lighting").Brightness < 1 then
plr.PlayerGui.ScreenGui.output.Text = "You are getting cold";
end
end)
(player).PlayerGui is the gui the player sees. StarterGui will not auto update or modify the game's players' guis when it is changed.
If you have any more questions feel free to ask them with a comment! ^-^
Roblox lua documentation:
https://www.developer.roblox.com/en-us

Related

Detect if someone is noclipping or flying(Roblox Lua)

So, I am making a game that is highly sensitive to hackers and if someone did hack it, it would be completely ruined. Is there any possible way to detect if someone is flying/noclipping and kick them using Roblox Lua scripts??
I found this answer on the devforums, but if a player is noclipping, they have no hitbox, meaning you can raycast from the HumanoidRootPart, and if that ray can't see the player, then they are noclipping however, some false positives may occur with laggy players so beware.
Anway the devforums are a better place to get your answer so go there instead.
i have not made anything in roblox before, so this might not work
one way to detect flying is the check the y of the player:
if y > 100 then -- y can be replaced with other variables, and i recommend you change 100 to another number
-- ban
end
you can also apply this to x and z.
this solution however only works if the player flies to a certain point, so you might have to get creative, i'll edit this answer when i finish reading the documentation
another way is by making an object out of bounds, when the player collides with this object it bans them.
that is all i can think of, let me know if you have any problems!

I'm trying to get "leaves" to produce a rustling noise in a Roblox game by monitoring all child parts of the script

Using the Child-monitoring script #Kylaaa helped me with in another question, I've spent hours on this, altering the core functionality from healing and disappearing bricks to play a parent sound when the player "walks through the leaves". I've been studying various aspects of the problem ('IsA', 'TouchInterest', 'If...then', Classes, Operators, etc.), and done various things, such as altering my script to look at the name of touched objects instead of their classes, but I've had no luck getting the sound to play.
-- create a helper function to access the brick and the thing that touched it
function OnTouched(thing)
local active = false --start with this debounce variable off so that the function will run the first time
return function(target) --provides actual target of the event
if active then return end -- do not do the animation again if it has already started
local player=thing.Parent:findFirstChild("Humanoid") --determine if it's humanoid
if player then --if it is, then
active = true --toggle to prevent function from running again simultaneously
script.Parent:play() --play rustling leaves sound
wait(1)
active = false -- reset so function can be used again
end --end of if statement
end
end
-- loop over the children and connect touch events
local bricks = script:GetChildren()
for i, brick in ipairs(bricks) do
if brick:IsA("Part") then
local onTouchedFunc = OnTouched(brick)
brick.Touched:Connect(onTouchedFunc)
end
end
This is a shot of the leaf litter I want to add the rustling sound to. I also have the same problem with the vines on the left side.
Vines:
All leaves are 'MeshParts' with 'SurfaceAppearance' inside, and are Anchored and CanTouch but not CanCollide. I tried turning on CanCollide but it didn't help.
Dense Leaf patch properties:
Surface Appearance properties:
I had tried "if brick:IsA("Model" or "MeshPart") then"
and
"if brick.Name=="Dense Leaf patch" or "SurfaceAppearance" or "DenseLeafPatch" then"
but neither of those worked.
I then noticed that a TouchInterest wasn't generated by Roblox, and I learned that you can neither copy nor duplicate TouchInterests to use where you need them (they're not there for developers, according to a message in the DevForum).
I originally had 1-3 leaves 'MeshPart's in separate 'Model's, but then made a single, invisible part (name: TouchInterest), CanTouch not CanCollide, that was large enough to cover almost all of the physical area of the instances, and put all of the MeshParts into that part. Then, I altered the script to just look for 'Part's. As a result, a TouchInterest does get generated during run-time.
This is all of the stuff involved in the group "leaves", with the sound, then the script, the containing part, then all the leaves inside as children of the script.
Sound's properties:
No errors for this were in the Output.
I feel like there's something simple I don't understand. Do I need several small parts (instead of the one large one) encompassing groups of leaves? Please help!
I figured out the problems.
On line 6, I used "thing" when I should've used "target".
On line 9, I forgot to capitalize "Play".
On line 10, I needed to make the wait 2 seconds instead of 1.

How do I use pathfinder to make the NPC chase a player

I already tried looking up multiple solutions in the Roblox developer forums, but I only found some unfinished code or some code that didn't work with mine. The code in this picture makes my rig find the part and chase it until it's there. That works perfectly, but I've been trying to find a way for the rig to chase the nearest player instead. I've heard of magnitude, but I'm not sure how to implement it, I cant even make the rig chase a player in the first place.
First off, .magnitude is the "length of a vector". It is mostly used to find the distance in vector units from pointA to pointB. Hence, the distance from the 2 points would be (pointA.Position-pointB.Position).magnitude.
https://developer.roblox.com/en-us/articles/Magnitude
Now in order to chase the players, you can loop through all of the players and get the one your NPC will chase.
You can use a for loop and loop through game.Players:GetPlayers() then get their character: <v>.Character and then their HumanoidRootPart which has their position. If you wanted to have a range for your NPC or an 'aggro distance', this is where you can implement magnitude. You would first create a variable for your distance. Since we will be dealing with vector units, it should be in length of vectors. For example local aggroDistance = 30. When added to the code, this would mean that it would only track a player if they are within 30 studs. You would then put an if statement saying if (<NPC.HumanoidRootPart.Position-<players hrp position>).magnitude < aggroDistance then. Now you could use Pathfinding Service to move the NPC to the player by using PathfindingService:ComputeAsync(<NPC HumanoidRootPart Position, <player HumanoidRootPart Position>) :ComputeAsync() makes a path from the starting position (the first parameter) to the end position (the second parameter). <NPC Humanoid>:MoveTo(<player HumanoidRootPart Position>) then makes the NPC move to the player. In order to listen out for when the NPC has reached the end of the path it made in :ComputeAsync() you can do <NPC Humanoid>:MoveToFinished:Connect(function()) to run a function after it reached the end, or <NPC Humanoid>:MoveToFinished:Wait() to wait before computing the next path.
Tip: You might also want to check if the player has more than 0 health (if they are alive) so the NPC only moves to players who are alive. You can do this by adding a and <player>.Humanoid.Health > 0 in the if statement where you had your aggroDistance.
Please let me know if you have any questions.
Code Makeup:
aggroDistance variable < optional
while loop
if statement (can contain aggroDistance variable if you have one) and check player health
:ComputeAsync()
:MoveTo()
:MoveToFinished
if statement end
while loop end

Solitiare card game - how to program resume game function?

I've been programming a solitaire card game and all has been going well so far, the basic engine works fine and I even programmed features like auto move on click and auto complete when won, unlimited undo/redo etc. But now I've realised the game cannot be fully resumed ie saved so as to continue from the exact position last time the game was open.
I'm wondering how an experienced programmer would approach this since it doesn't seem so simple like with other games where just saving various numbers, like the level number etc is sufficient for resuming the game.
The way it is now, all game objects are created on a new game, the cards, the slots for foundations, tableaus etc and then the cards are shuffled and dealt out. This is random but the way I see it, the game needs to remember this random deal to resume game and deal it again exactly the same when the game is resumed. Then all moves that were executed have to be executed as they were as well. So it looks like the game was as it was last time it was played, but in fact all moves have been executed from beginning again. Not sure if this is the best way to do it but am interested in other ways if there are any.
I'm wondering if any experienced programmers could tell me how they would approach this and perhaps give some tips/advice etc.
(I am going to assume this is standard, Klondike Solitaire)
I would recommend designing a save structure. Each card should have a suit and a value variable, so I would write out:
[DECK_UNTURNED]
H 1
H 10
S 7
C 2
...
[DECK_UNTURNED_END]
[DECK_TURNED]
...
[DECK_TURNED_END]
etc
I would do that for each location cards can be stacked (I believe you called them foundations), the unrevealed deck cards, the revealed deck cards, each of the seven main slots, and the four winning slots. Make sure however you read them in and out, they end up in the same order, of course.
When you go to read the file, a simple way is to read the entire file into a vector of strings. Then you iterate through the vector until you find one of your blocks.
if( vector[ iter ] == "[DECK_UNTURNED]" )
Now you go into another loop, using the same vector and iter, and keep reading in those cards until you reach the associated end block.
while( vector[ iter ] != "[DECK_UNTURNED_END]" )
read cards...
++iter
This is how I generally do all my save files. Create [DATA] blocks, and read in until you reach the end block. It is not very elaborate, but it works.
Your idea of replaying the game up to a point is good. Just save the undo info and redo it at load time.

What is ROBLOX Lua scripting? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I really dont understand what it really even is. Is it just normal scripting or something else?
Lua is a fairly well known and often embedded scripting language.
However, if you're after some basic "getting starting" information on Roblox scripting, check out the Roblox Wiki. (The tutorial's section would probably be of specific interest.)
Lua is a well known scripting and programming language that is lightweight and easy to learn. Many games have embedded it, including Garry's Mod (GMod) and World of Warcraft.
ROBLOX uses Lua to actually create games. Most of the features you see in ROBLOX (like the GUI and building tools) are actually coded in Lua.
I'd recommend seeing games by Anaminus, VolcanoINC and Telamon to see what you could do with Lua.
Lua is a scripting language somewhat similar to Java. Infact, I recall there being a Javalua blend as a scripting language in itself. Lua is probably the easiest scripting language to learn and work with. Its functions are fired by changes specified such as script.Parent.Value.Changed:connect(functionnamehere)
Parents are what the script or item specified is in.
Variables work like this:
v = script.Parent.Value
or
d = game.Workspace.ScriptFireValue.Value
If a ROBLOX Solo Game is the source and v's script.Parent's name (script.Parent.Name) is ScriptFireValue then v is equal to d.
The language also includes loops that are recognizable like
lua: while true do
vbs: do while/Loop
java: do while
'for' is a limited loop where it only loops for a certain amount of times.
exe.
for i = 1, 10 do
game.Lighting.TimeofDay = game.Lighting.TimeofDay + 1
end
This part of the script will run 10 times before passing on. when u have the part 1 - 10 or 1, 10.
The 'end' comes after anything highlighted in blue.
Things highlighted will be:
for [whatever's in here will not be highlighted] do - Both words only count for one end.
while true do
while [Something in here that exists or is a value] do - Both words only count for one end.
function()
if [something exists or is a value] then - Both words only count for one end.
else -- Used when the if statement before it is false. When used the 'if' and the 'else' count for one end.
elseif -- Used when the if statement before it is false but also calls for another if statement. When used the 'if' and the 'elseif' count for one end.
I think a few more.
Here's an example script I'm writing off the top of my head. The source I'm going off of is ROBLOX's Build/Edit mode in-game.
function KillAllPlayers(clicker)
if clicker.Name == "coolboy10000" then
people = game.Players:GetChildren()
for i = 1, #people do
people[i].Character.Humanoid.Health = people[i].Character.Humanoid.Health - 10000
end -- ends if
end -- ends for - do
end -- ends function
script.Parent.Clicked:connect(KillAllPlayers)
That script if not obvious identified the player who clicked. (clicker). Btw the argument 'clicker' would be identified the cause of the function to be fired. So the cause is because a button was 'Clicked'. So 'clicker' retrieves the person who initiated that. Therefore identifying if the player is a certain person which would allow the process to continue. So if the player's name is coolboy10000 then it will gather all the players and kill them each.
To put a security on that button to where if the player is not coolboy10000 then the player will be killed you could do this:
function KillAllPlayers(clicker)
if clicker.Name == "coolboy10000" then
people = game.Players:GetChildren()
for i = 1, #people do
people[i].Character.Humanoid.Health = people[i].Character.Humanoid.Health - 10000
end -- ends for - do
else
clicker.Humanoid.Health = clicker.Humanoid.Health - 10000
end -- ends if and else
end -- ends function
script.Parent.Clicked:connect(KillAllPlayers)
If there are multiple people to allow to do this function you could do:
function KillAllPlayers(clicker)
if clicker.Name == "coolboy10000" or "coldnature" then
people = game.Players:GetChildren()
for i = 1, #people do
people[i].Character.Humanoid.Health = people[i].Character.Humanoid.Health - 10000
end -- ends for - do
else
clicker.Humanoid.Health = clicker.Humanoid.Health - 10000
end -- ends if and else
end -- ends function
script.Parent.Clicked:connect(KillAllPlayers)
Or if there is a specific person who should have a separate punishment:
function KillAllPlayers(clicker)
if clicker.Name == "coolboy10000" or "coldnature" then
people = game.Players:GetChildren()
for i = 1, #people do
people[i].Character.Humanoid.Health = people[i].Character.Humanoid.Health - 10000
end -- ends for - do
elseif clicker.Name == "Person299" then
clicker.Head.Position = clicker.Torso.Position
else
clicker.Humanoid.Health = clicker.Humanoid.Health - 10000
end -- ends if and else and elseif - then
end -- ends function
script.Parent.Clicked:connect(KillAllPlayers)
Yeah that's just the basics :/
There are tutorials out there. Mostly on ROBLOX free models. I say you should study some free scripts and learn how they work and stuff. This is just the basics. There is a tutorial on ROBLOX. Just search in Free Models scripting tutorials. Some dude wrote in scripts how to script. It's pretty long to read but that's how I learned.
Roblox is a gaming website where the users make the games using "Roblox Studio". It's almost like a super complex virtual Lego. To interact with your parts(anything in your game) you make scripts that are written in the language "Lua".
Roblox Lua is Lua 5.1 in Roblox's Data Model.
Roblox Lua Scripting is the act of writing in a script in Roblox Studio.
Their scripts are actually objects with embedded code inside of them. They're placed inside of roblox's basic data model and are used for creating and controlling objects, data, and therefore game-play.
I will not repeat what others have said, and say something else instead.
Unlike vanilla lua, ROBLOX lua (also known as rlua) is a modified version of lua.
ROBLOX has implemented different kinds of c and l closures such as tick, wait, delay, and so on, which is why it is a modified version of lua.
Roblox Lua scripting is an embedded coding language to add features to your game. It is easy to learn and is a loose interpretation of modern day game programming. Its an overall great language and I highly recommend it!
https://roblox.fandom.com/wiki/Project:Home
https://devforum.roblox.com/

Resources