Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Red light running fine script doesn't subtract $300 from the player's $$$ leaderstat?

Asked by 6 years ago

So, in my game, if you run a red light, you get a $300 fine. What triggers this is a person passing through a detector while the light it is below is red. At least, that's what supposed to happen. It is not happening. Why??? No output errors or anything, it just doesn't work. The script is a LocalScript that is inside of the detector.

1local Part = script.Parent
2Part.Touched:Connect(function(hit)
3    if hit.Parent:FindFirstChild("Humanoid") and script.Parent.Parent.TrafficLight1.TSM.Red.PointLight.Brightness == 8 then
4        game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value - 300
5    end
6end)
0
In regards to your latest issue, you might want to change the number in wait(1.5) to a larger number. If you're driving really slowly, there is a chance it will trigger multiple times for the same person. If the person stops the car inside of the part and stay there, then moves after the 1.5 seconds again, it may run also. Touched event if finicky like that, there is no clean solution for it. EzraNehemiah_TF2 3552 — 6y
0
If you're in studio mode and you change the money value to 500 through studio mode, then the changes will not stick. The script is a server script! Changes through studio will not affect the server script and the server script thinks you're starting with 0 cash. EzraNehemiah_TF2 3552 — 6y
0
https://i.gyazo.com/6f1e00493773c19de79bdf1eb8948916.png Make sure you click on "Current: Client" to change it to server. Then click on it again to switch back to playing mode after you changed the cash. EzraNehemiah_TF2 3552 — 6y
0
Ok, thank you. I will try this script in the actual game, not in studio test mode. sesamert16 31 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

Local Scripts

The point of local scripts is to make edits to something only a client can see. UI or local parts can only be seen by a single client, so those are edited via local scripts. If you're making changes to the game, you need to use a server sided (or normal)


How to Get Player with a Server Script

Use GetPlayerFromCharacter. The argument is the player's character and it will return the character.

1local Players = game:GetService("Players")
2 
3local player = Players:GetPlayerFromCharacter(Character) --"Character" will be the model
4if player then --If the player is real then...
5    print(player.Name.." is a player.")
6else --If the player is not real then...
7    print("Not a player...")
8end

Implementing with a Touched event

Here's how your code would look like if you combined it with GetPlayerFromCharacter.

1local Part = script.Parent
2Part.Touched:Connect(function(hit)
3    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
4    if player and script.Parent.Parent.TrafficLight1.TSM.Red.PointLight.Brightness == 8 then
5        player.leaderstats.Money.Value = player.leaderstats.Money.Value - 300
6    end
7end)

Debouncing

If you were to use the code as is, you'd notice that you are losing a lot more cash than just 300. The reason is because this code is being run once for every single body part and cosmetic the player is wearing. Meaning, if a player is wearing 3 hats, he could lose up to $2,700 at once! If you put a debounce, it will prevent the code from running again until a certain condition is met. In this example, the condition is time:

01--Button can only be pressed once every 5 seconds
02local debounce = false --can be named anything; variable doesn't have to be a bool, it's just easier that way
03 
04script.Parent.Touched:Connect(function()
05    if not debounce then --If debounce is false then...
06        debounce = true --Make it true, so this script wont run again!
07        print("Activated")
08        wait(5) --After 5 seconds, make debounce false again
09        debounce = false --Now the script is ready to be pressed again
10    end
11end)

Final Product

01local Part = script.Parent
02local debounce = false
03 
04Part.Touched:Connect(function(hit)
05    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
06    if not debounce and player and script.Parent.Parent.TrafficLight1.TSM.Red.PointLight.Brightness == 8 then
07        debounce = true
08        player.leaderstats.Money.Value = player.leaderstats.Money.Value - 300
09        wait(1.5) --or however long it takes for the player to cross the red light in seconds
10        debounce = false
11    end
12end)


Hope it helps!

1
Works great! ty! sesamert16 31 — 6y
0
Wait, one issue. I just set the money value to 500, and it made it -300 depsite the fact that 500 - 300 is 200. Sorry for not realizing that earlier, the previous times I had tested it the money value was at 0 so -300 was the correct answer. sesamert16 31 — 6y
Ad

Answer this question