function onTouch(part)
local human = part.Parent:findFirstChild("Humanoid")
if (human == nil) then
return
end
human.Health = human.Health - 10
end
script.Parent.Touched:connect(onTouch)
I'm new to coding in lua, and it is my first time using functions. I want to know what the "part" is equal to so that I can find out how to set up the human variable
local human = part.Parent:findFirstChild("Humanoid")
without using the "part," like what can I plug in so that works without even setting up part, because I want to do something with it in a loop:
local burnaffect = false
--local a = 0
function onTouch(part)
local human = part.Parent:findFirstChild("Humanoid")
if (human == nil and burnaffect == false) then
return
end
a = 0
burnaffect = true
end
script.Parent.Touched:connect(onTouch)
while burnaffect == true do
local part = --????
local human = part.Parent:findFirstChild("Humanoid")
human.Health = human.Health - 10
end
The code may seem confusing but I'm fairly new so I don't know what is best yet.
It looks like what you're trying to do is have a player "set on fire" when they touch a certain brick. The code below does exactly this, and I'll explain it line by line afterwards.
function onTouch(part)
local human = part.Parent:findFirstChild("Humanoid")
if (human == nil) then
return
end
local fire = Instance.new("Fire", part)
while (human.Health > 0) do
human.Health = human.Health - 10
wait(0.1)
end
end
script.Parent.Touched:connect(onTouch)
So, I'll start going through this.
function onTouch(part)
We need to define the function first and give it a name so that we can reference it in the Touched event later. The part parameter is the Part object that touched the script.Parent object and caused the Touched event to fire. So, ROBLOX will automatically call this function whenever something touches your script.Parent and automatically input the Part that touched it as that first parameter, part.
local human = part.Parent:findFirstChild("Humanoid")
This will get the Parent of the Part that touched the block (because if a player is touching the block, it isn't going to give us the Character, it's going to give us an Arm or a Leg in the part variable, because that's the actual Part that touched it, so we need to get that part's Parent. Then, once we have the Parent, we want to get the Humanoid object inside of the Character. Then, we put that Humanoid object inside of the human variable (if we could find one, otherwise we put nil into that human variable).
if (human == nil) then
We want to check if human is nil (which would mean we couldn't find a Humanoid object in the line before this one, which means whatever touched this isn't a real Character, so we'll want to return (which means stop running the function immediately).
local fire = Instance.new("Fire", part)
This line isn't necessary, I added it because I thought if you wanted to simulate burning, this would help. You can leave it out if you'd like. It will create a new Instance of type Fire, and places it inside of the part. That is to say, if a player's Leg touches this part, a Fire will be put inside of that leg, which will make it appear to ignite in flames.
while (human.Health > 0) do
We want to keep looping until the player dies (has a human.Health value of 0 or less) so we tell the loop to keep going while human.Health is greater than 0.
human.Health = human.Health - 10
We'll deincrement the human.Health value by 10, and then wait(0.1), which will cause the script to wait for 1/10 of a second (you can change this to a different number, but it is important to keep it as if you remove it, the loop will run extremely fast and kill the player immediately. If you want this to happen, you can remove the wait, but if you wanted to kill the player immediately, you could just set human.Health = 0 in the first place.
If you have any questions, feel free to ask! Hope this answered your question.
I remember when I used to use Lua for Roblox. The part is the part in the game that gets touched. You need to reference it so you can find the humanoid object it belongs to or lack of so your code can tell if it is a humanoid that touched it or not. Let me know if you have any further questions.
Related
The button, which I'm trying to use, is in StarterGUI. Don't know if it helps. Fairly new to scripting, however saw another post with something similar and used that code. Here is my code inside the LocalScript:
local MarketplaceService = game:GetService("MarketplaceService")
local player = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.PurchaseEvent
local productID = 1218445531
script.Parent.MouseButton1Click:Connect(function()
PurchaseEvent:FireServer(productID)
end)
if MarketplaceService:PlayerOwnsAsset(1218445531) then
--Here is where the thing is supposed to happen
end
First you will need to get a reference to the part, one way is to use something like local part = workspace:WaitForChild("PartName")
In order to make it visible, you need to set the transparency to 0. If you dont want the player walking through the part you may also need to turn collisions on with CanColide. Anchoring the part is also recommend to prevent it from falling.
part.Transparency = 0; -- makes it visible
part.CanColide = true; -- makes sure other objects cant go through this
part.Anchored = true; -- prevents the object from falling
There are many ways to make a part move. If the part is supposed to go from one place to another, and only has one destination, a Tween might be useful (see TweenService).
Another way would be with a Heartbeat loop that changes the position each frame (see RunService.Heartbeat). For example,
local keepMoving = true; -- change this to false to stop moving
local RunService = game:GetService("RunService");
local direction = Vector3.new(0, 1, 0); -- change this to anything you want
while (keepMoving) do
local timePassed = RunService.Heartbeat:Wait(); -- wait one frame, roughly 0.016 of a second.
part.Position += direction * timePassed; -- move the part in the direction
end
This would move part up 1 stud every second.
So I have a roblox puzzle game that I have been working on for a while now on based on the arcade classic Q bert where the goal is to change all the colours of the bricks while avoiding enemies and getting a high score but I will be adding some features of my own so it does not get as repetitive such as additional tasks like collecting keys on platforms to unlock the door to the next level and secrets like a diamond that rarely appears once every 10 rounds and collecting one gives the player an extra dude and 10 million points.
This is how the game looks so far https://streamable.com/na46cu
The issue I am having as you can see is that the colours do in fact change but when I jump on it again it changes back to the first colour it changes to which in this case is green but I want it to stay on the first colour and make it so that it does not change until the player jumps on the brick again and later on in the game I want it to become more complex and puzzle like as the game goes on like in this example [https://www.youtube.com/watch?v=9eXJWiNXpOo][2] .
I have tried a few things like adding timers ,debounces and even separate scripts alltogether none of which have worked out for me so far and I of course went out looking for a question from somebody else that had a similar problem but so far I have been struggling to find anybody else that has the same problem.
local module = {} --module for the modulescript and for loop is created
local CollectionService = game:GetService("CollectionService")
for _, part,brick in pairs (CollectionService:GetTagged("blocks"))
do
part.Touched:Connect(function(hit) --Part connects with the touched property to the function with the parameter hit
if (hit.Parent:FindFirstChild("Humanoid"))
then
part.BrickColor = BrickColor.new ("Bright green")
wait (2)
part.BrickColor = BrickColor.new ("Eggplant")
-- local sound = workspace.Sound -- use "local sound = workspace.Sound", if there is already a sound object in the workspace
--sound.SoundId = "rbxassetid://4797903038" --replace quoted text with whatever sound id you need to use
--sound:Play()
end
end)
end
-- end)
--end
return module
I am not the best programmer but I do know the basics of programming and I have tried out various programming languages like Python and c++ all of which are not all that hard to understand once you know the basics of it all but finding out the solution to the problem is the really tricky part and so is bug fixing and troubleshooting.
I do know I could try a simple debounce system but that still does not solve the problem and it only makes it so the code only runs once and slows it down.
I have been asking all over the place for a solution to this problem but I never got an answer to it so I am trying on good old Stackoverflow for once to see if this will be the place where I get the help I need.
this should work, try it
local module = {} --module for the modulescript and for loop is created
local CollectionService = game:GetService("CollectionService")
local DidParts = {} -- Initializing another table to check if the part is already in it
for _, part,brick in pairs(CollectionService:GetTagged("blocks")) do
part.Touched:Connect(function(hit) --Part connects with the touched property to the function with the parameter hit
if hit.Parent:FindFirstChild("Humanoid") then
if table.find(DidParts,part) then
return -- checking if the part isnt in the table if it is then return
end
part.BrickColor = BrickColor.new("Bright green")
wait(2)
part.BrickColor = BrickColor.new("Eggplant")
table.insert(DidParts,part) -- when all the code has finished insert it in the table
-- local sound = workspace.Sound -- use "local sound = workspace.Sound", if there is already a sound object in the workspace
--sound.SoundId = "rbxassetid://4797903038" --replace quoted text with whatever sound id you need to use
--sound:Play()
end
end)
end
-- end)
--end
return module
can someone help me on how show i move the NPCs through a path, like in the tds game?. I have tried this
local function move()
-- source is an array with all the positions it has to go to
for i=1,#source,1 do
-- This is in case the MoveTo takes more than 8 seconds
local itreached = false
while itreached == false do
npc.Humanoid:MoveTo(source[i].Position)
wait()
npc.Humanoid.MoveToFinished:Connect(function()
itreached = true
end)
end
end
end
and it works to an extend , that when i approach the npc it somehow falls and lags , otherwise if i just run it without a player it works just fine. Are there other techniques, like lerp or tween? I tried using lerp, but i couldn't move the whole model.
video showing the problem
You are running into an issue with network ownership. The Roblox engine decides who is responsible for calculating the positions of objects based on some calculation around who is closest to the object and who has a strong enough machine to do the calculation. For example, desktop computers and laptops tend to have a wider sphere of influence than mobile devices. Anyways, when you walk close it the NPCs, there is a handoff of ownership, and that is causing the NPCs to fall over. So to fix it, you need to call SetNetworkOwner(nil) on the NPC's PrimaryPart so that the part is owned by the server.
npc.PrimaryPart:SetNetworkOwner(nil)
Also, if you want to clean up your code, you can make it entirely driven by events. Once you tell it to start moving, it will select the next target once it arrives.
local targetPos = 1
local function move()
npc.Humanoid:MoveTo(source[targetPos].Position)
end
-- listen for when the NPC arrives at a position
npc.Humanoid.MoveToFinished:Connect(function(didArrive)
-- check that the NPC was able to arrive at their target location
if not didArrive then
local errMsg = string.format("%s could not arrive at the target location : (%s, %s, %s)", npc.Name, tostring(targetPos.X), tostring(targetPos.Y), tostring(targetPos.Z))
error(errMsg)
-- do something to handle this bad case! Delete the npc?
return
end
-- select the next target
targetPos = targetPos + 1
-- check if there are any more targets to move to
if targetPos <= #source then
move()
else
print("Done moving!")
end
end)
I am making a game that has different walkspeeds in different sections, but I don't want people to change their own walkspeed with hacks. My solution was to make a part and stretch it to fit the entire area and make it invisible + CanCollide disabled, and then use the child script to kill you if your walkspeed isn't what it should be:
script.Parent.Touched:connect(function(WSChecker)
if not WSChecker.Parent then return end
local humanoid = WSChecker.Parent:FindFirstChild("Humanoid")
if not humanoid then return end
if (humanoid.WalkSpeed ~= 25) then
humanoid.Health = 0
end
end)
Problem is that it does not work with multiple players in the part at one time, and I want to make it so it will kick the player instead of killing them. Is there a way to go about these problems? It has to check their ws only within the part, and I don't know how to make it kick whoever changed their ws instead of killing them.
I would suggest hooking up your function to each player's Humanoid instead, and use the Humanoid.Running event.
Humanoid.Running provides you the speed they're currently running at, which means you can check if that speed is ever above a certain threshold, and punish them if it is.
Code example:
player.Character.Humanoid.Running:Connect(function(Speed)
print(Player, "is running at speed", Speed)
end)
As for kicking, you want to use player:Kick().
if (humanoid.WalkSpeed ~= 25) then
game.Players.LocalPlayer:Kick()
end
that should do the trick...
I think I can help. The solution that worked for me is:
local player = game.Players.LocalPlayer --Make sure it's a local script.
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local hum2 = char.Humanoid
script.Parent = game.StarterPlayer.StarterPlayerScripts
if hum.WalkSpeed >16 then
player:Kick('You have been kicked for possible speed hacks.')
end
if hum2.WalkSpeed >16 then
player:Kick('You have been kicked for possible speed hacks.')
end
if (humanoid.WalkSpeed ~= 25) then
game.localplayer.remove:fire
end
end)
Hopefully, this is a better solution.
You can ofcourse do the above but as an addition to what everyone above said, checking if the walkspeed value is at the desired value is easily bypassable.
Exploiters will get the raw metatable of the game and hook __index to return a normal value for WalkSpeed. Metatables cant be detected either as most modern exploits use C closures instead of Lua. Your best chance is to see how fast the character is moving and teleport them back if player is moving too fast (like a passive anticheat).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Like in the title, i am looking for a script that can do that. If someone can repair this script I'll be happy :D
function onTouched(part)
local h = part.Parent:findFirstChild("Humanoid")
if h ~= nil then
if player.parent.torso.roblox.Texture == "https://web.roblox.com/Bloxxer-item?id=1028595" then
script.Parent.Check.Transparency = 0
wait (2)
script.Parent.Check.Transparency = 1
end
end
end
script.Parent.Touched:connect(onTouched)
If you cannot find any free-model that either does what you want, or are editable; here we go:
Because we are referencing script.Parent often, lets make a variable:
local Block = script.Parent
Also lets avoid putting constants like urls in the code body by making variable for that too:
local TShirtTexture = "http://www.roblox.com/asset/?version=1&id=1028594"
Note that the t-shirt texture link differs from the item link. I think the Bloxxer texture is http://www.roblox.com/asset/?version=1&id=1028594. To find the link, join the game in studio with the t-shirt on and inspect the t-shirt
We also need a debouncer
Then i like anonymous functions better if you do not really need to reference it outside:
Block.Touched:connect(function(Part)
-- code goes here
end)
The line part.Parent:findFirstChild might be unsafe as part.Parent might be nil if the part was removed after touching but before the code runs, so it is best to check it first (some VIP doors used to break if you shoot at them because of this). Same with the other stuff, check that it exists, else the code might break at some point.
Next, the character Torso is capitalized. Also, the t-shirt is in the character, not the player. And throw in some variables
And finally everything together:
-- Put some variables
local Block = script.Parent
local TShirtTexture = "http://www.roblox.com/asset/?version=1&id=1028594"
-- Debounce variable
local Debounce = false
-- Use anonymous function
Block.Touched:connect(function(Part)
-- Assume that the Part is a BodyPart, thus the parent should be the character
local Character = Part.Parent
-- Ensure that our assumption is correct
if Character == nil or Character:findFirstChild("Humanoid") == nil then return end
-- Reference to the assumed Torso
local Torso = Character:findFirstChild("Torso")
-- Ensure that it exists
if Torso == nil then return end
-- Reference to the assumed T-Shirt
local TShirt = Torso:findFirstChild("roblox")
-- Ensure that it exists, and that it is a Decal (T-Shirts are decals)
if TShirt == nil or not TShirt:IsA("Decal") then return end
-- Ensure the texture is correct
if TShirt.Texture ~= TShirtTexture then return end
-- Check debounce
if Debounce then return end
Debounce = true
-- Do stuff
Block.Check.Transparency = 0
wait (2)
Block.Check.Transparency = 1
Debounce = false
end)
And if you want to check if the player owns the item, but not have to wear it, check this out
Also, if scripts does not work, remember to post relevant errors