Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

local Part = script.Parent
Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and script.Parent.Parent.TrafficLight1.TSM.Red.PointLight.Brightness == 8 then
		game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value - 300
	end
end)
 

4 comments

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.

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.

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.

0

Ok, thank you. I will try this script in the actual game, not in studio test mode.

1 answer

✓ 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) --"Character" will be the model
if player then --If the player is real then...
	print(player.Name.." is a player.")
else --If the player is not real then...
	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:

--Button can only be pressed once every 5 seconds
local debounce = false --can be named anything; variable doesn't have to be a bool, it's just easier that way

script.Parent.Touched:Connect(function()
	if not debounce then --If debounce is false then...
		debounce = true --Make it true, so this script wont run again!
		print("Activated")
		wait(5) --After 5 seconds, make debounce false again
		debounce = false --Now the script is ready to be pressed again
	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) --or however long it takes for the player to cross the red light in seconds
		debounce = false
	end
end)


Hope it helps!

2 comments

1

Works great! ty!

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.