What I’m trying to do is when the player goes over the speed limit with there car they will get a Ticket. What I have done here it’s not working and idk why but I’ve tried so many ways and nothing is working.
Server Script
local Gui = script:WaitForChild("Ticket")
local speedLimit = script.Parent.Parent.Parent.SpeedLimit
local debounce = false
script.Parent.Touched:Connect(function(player)
if debounce == false then
debounce = true
if player.ClassName == "DriveSeat" then
print(math.floor(player.Velocity.magnitude))
if player.Velocity.magnitude >= speedLimit.Value then
local clone = Gui:Clone()
clone.Parent = player.PlayerGui
player.leaderstats.Pounds.Value = player.leaderstats.Pounds.Value - 100
player.leaderstats.Bounty.Value = player.leaderstats.Bounty.Value + 7
else
end
end
end
end)
If someone can point out in my script that would be great!
There are a few issues with your script that could be causing it to not work as intended.
First, you have a local variable called “debounce” that is set to false at the beginning of the script. However, you are not resetting the value of “debounce” to false at any point in the script, so it will always remain true after the first time the script is run. This means that the script will only run once, and then it will never run again because “debounce” will always be true.
Second, you are using the “Touched” event to detect when the player’s car touches the script. However, this event is triggered when any object touches the script, not just the player’s car. This means that the script could potentially be triggered by other objects in the game, which may not be what you want.
To fix these issues, you could try the following changes to your script.
Reset the value of “debounce” to false after the script has run once. You could do this by adding a line of code at the end of the script that sets “debounce” to false, like this:
debounce = false
Use a different event to detect when the player’s car touches the script. Instead of using the “Touched” event, you could use the “Collision” event, which is triggered when two objects collide with each other. You would need to add a “Part” object to the script, and then use the “Collision” event on that object to detect when the player’s car touches the script.
Here is an example of how you could rewrite your script using the “Collision” event.
local Gui = script:WaitForChild("Ticket")
local speedLimit = script.Parent.Parent.Parent.SpeedLimit
local part = script:WaitForChild("Part")
part.Collision:Connect(function(otherPart)
if otherPart.Parent:IsA("DriveSeat") then
local player = otherPart.Parent.Parent
print(math.floor(player.Velocity.magnitude))
if player.Velocity.magnitude >= speedLimit.Value then
local clone = Gui:Clone()
clone.Parent = player.PlayerGui
player.leaderstats.Pounds.Value = player.leaderstats.Pounds.Value - 100
player.leaderstats.Bounty.Value = player.leaderstats.Bounty.Value + 7
end
end
end)
I hope this helps! Let me know if you have any questions.