Hello, I am very new to scripting and was wonder how do I use getConnectedParts? Or better, What do I do with pre-made functions.
What I am trying to do:
When block A touches block B a value will decrease.
Hello, I am very new to scripting and was wonder how do I use getConnectedParts? Or better, What do I do with pre-made functions.
What I am trying to do:
When block A touches block B a value will decrease.
No need to use the messy getConnectedParts() method!! Just use the Touched event. Here's an example
local blockA = game.Workspace.blockA --Set this to blockA
local blockB = game.Workspace.blockB --Set this to blockB
local val = game.Workspace.ValueThing --Set this to value to decrease
local decrement = 1 --Set this to how much the value should decrease by
function decrementValue() --Define a function
val.Value = val.Value - decrement -- Decrease the val by decrement value
end --End function
blockA.Touched:connect(function(hit) --When blockA it hit, this will fire
if hit == blockB then --Check if the part blockA touched was, in fact, blockB
decrementValue() --Call function to decrement value
end --End if
end) --End handler function
[EDIT] I edited this so that the decrementing could be a single function to be called anywhere.
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
A couple quick questions, should I use ";" and how do I call this function if I have to?
The semicolon is not necessary in Lua, and is, at times, a bad thing, so no, don't use it.
As for your second question - this isn't a function. It's a handler. I'll edit my answer for making it a call-able funciton.