I am trying to make a script that when you touch a part you get 100 points but for 1. it doesn't see that there is a wait and it gives the points at the speed of light and 2. it does the loop for more than one time (witch is not suposed to happen)
Here is the script:
1 | for i = 1 , 1 , 1 do |
2 |
3 | wait( 5 ) |
4 | game.Workspace.points.Touched:Connect( function () |
5 | game.Players.walvie.leaderstats.points.Value = game.Players.walvie.leaderstats.points.Value + 100 |
6 | end ) |
7 | end |
PS:game.Players.walvie.leaderstats.points.Value = game.Players.walvie.leaderstats.points.Value + 100 is on the same line.
GENERAL PRACTICE
Your script will only work for your player, so you will want to define the player through the Player Event :GetPlayerFromCharacter()
--- The player's character will be the hit.Parent
Your wait time can go into the function, rather than requiring a for-loop
ISSUES
You need a debounce to make sure the .Touched Event doesn't keep firing every time the part is touched (causing rapid gains after 5 seconds)
REVISED SERVER SCRIPT
01 | local debounce = true |
02 |
03 | workspace.points.Touched:Connect( function (hit) |
04 | if debounce = = false then return end |
05 | debounce = false |
06 | local player = game:GetService( "Players" ):GetPlayerFromCharacter(hit.Parent) |
07 | local points = player:WaitForChild( "leaderstats" ):WaitForChild( "points" ) |
08 | points.Value = points.Value + 100 |
09 | wait( 5 ) |
10 | debounce = true |
11 | end ) |
If you want the player to only ever receive the bonus once, you can just not reset the debounce
01 | local debounce = true |
02 |
03 | workspace.points.Touched:Connect( function (hit) |
04 | if debounce = = false then return end |
05 | debounce = false |
06 | local player = game:GetService( "Players" ):GetPlayerFromCharacter(hit.Parent) |
07 | local points = player:WaitForChild( "leaderstats" ):WaitForChild( "points" ) |
08 | points.Value = points.Value + 100 |
09 | wait( 5 ) |
10 | end ) |
01 | local buttonPressed = false |
02 | --Store whether the button is pressed in a local variable |
03 |
04 | game.Workspace.points.Touched:Connect( function (hit) |
05 | if not buttonPressed then |
06 | -- Is it not pressed? |
07 |
08 | buttonPressed = true |
09 | -- Mark it as pressed, so that other handlers don't execute |
10 |
11 | game.Players.walvie.leaderstats.points.Value = game.Players.walvie.leaderstats.points.Value + 100 |
12 | -- Do Stuff |
13 |
14 | buttonPressed = false |
15 | -- Mark it as not pressed, so other handlers can execute again |
16 | end |
17 | end ) |
From the Roblox Wiki with a few modifications to work for you.