✓ Accepted answer
#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.
local Players = game:GetService("Players")
local player = Players:GetPlayerFromCharacter(Character)
if player then
print(player.Name.." is a player.")
else
print("Not a player...")
end
#Implementing with a Touched event
Here's how your code would look like if you combined it with GetPlayerFromCharacter.
local Part = script.Parent
Part.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player and script.Parent.Parent.TrafficLight1.TSM.Red.PointLight.Brightness == 8 then
player.leaderstats.Money.Value = player.leaderstats.Money.Value - 300
end
end)
#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:
local debounce = false
script.Parent.Touched:Connect(function()
if not debounce then
debounce = true
print("Activated")
wait(5)
debounce = false
end
end)
#Final Product
local Part = script.Parent
local debounce = false
Part.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if not debounce and player and script.Parent.Parent.TrafficLight1.TSM.Red.PointLight.Brightness == 8 then
debounce = true
player.leaderstats.Money.Value = player.leaderstats.Money.Value - 300
wait(1.5)
debounce = false
end
end)
Hope it helps!
4 comments
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.
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.
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.
Ok, thank you. I will try this script in the actual game, not in studio test mode.