I'm trying to make a script that when you touch a certain block, it adds 1 (falls) to the leaderboard. I have no idea how to script it so pls i need help.
Hi! This is very simple! Just do the following:
Make a script under the block you want touched and put this in it
1 | script.Parent.Touched:Connect( function (obj) |
2 | if not obj.Parent:FindFirstChild( "Humanoid" ) then return end |
3 | game.Players:GetPlayerFromCharacter(obj.Parent).leaderstats.falls.Value = game.Players:GetPlayerFromCharacter(obj.Parent).leaderstats.falls.Value + 1 |
4 | end ) |
Short and sweet. With any further questions, feel free to reply to my post. Happy scripting! ~mc3334
01 | debounce = true -- make the part's function active when touched |
02 |
03 | function touched(hit) -- change "touched" to anything you want to be organized, "hit" can also be "part" or "player" whatever you want - it will be use in the function script as the player's Character body part that touched the part |
04 | if (hit.Parent:findFirstChild( "Humanoid" ) ~ = nil and debounce = = true ) then |
05 | debounce = false -- make the function not fire multiple times. If you don't turn debounce to false once the Character touches it, then it will give value to the leaderboard multiple times instead of only one value per touch. |
06 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- find the Player from its Character that touched the part |
07 | local falls = player.leaderstats.falls --tell where the "falls" is located in the game if you already put it inside the leaderstats/leaderboard script or a ClassName-"NumberValue" with name "falls" as a child of your leaderboard script |
08 | falls.Value = falls.Value + 1 -- 1 being the number being added to the falls value or change it to whatever number you want to add to its value |
09 | wait( 2 ) -- how long in seconds the part can be touch again to give away falls/points |
10 | debounce = true -- make the part's function to be able to give away falls/points again |
11 | end -- closing of the scripting "if,then" |
12 | end -- closing of the scripting "function touched(hit)" |
13 |
14 | script.Parent.Touched:Connect(touched) -- connect the "function touched" to the part being touched. The capitalized "Touched" here is how it is supposed to be spelled as it is the event in Roblox's scripting. Lowercase "touched" is from the name you give it in your "function touched(hit)". If you name it "function ontouch(hit)", then in this line, you should type it as script.Parent.Touched:Connect(ontouch) instead. |
Maybe learn to script and this is scripting helpers, not script makers.It really explains ur -2 reputation.