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)
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)
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
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)
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)
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!