So I'm trying to make a "Speed detector" for cars that go over the speed limit, if they touch the part and are over the speed limit they get a fine for speeding (100 HRK). Everything works except when I want to check the speed, I'm not sure how to do it. I use the A-Chassis System for my cars. Also, I'm unsure if I can check the WalkSpeed if the player is sitting.
How can I fix this or how can I make the Speed detector, it's really important and it means a lot to me if you help me out. Thanks!
db = false script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if db == false then db = true script.Parent.BrickColor = BrickColor.new("Bright red") player.leaderstats.HRK.Value = player.leaderstats.HRK.Value - 100 wait(3) script.Parent.BrickColor = BrickColor.new("Bright green") db = false end end end)
VehicleSeats have a velocity property which then you can get the magnitude by doing...
Velocity.Magnitude
The returned value will be the speed that the car seat is driving at. Here's an example.
local db = false workspace.Part.Touched:Connect(function(p) if p.Parent:FindFirstChildWhichIsA("Humanoid") then if db == false then db = true local hrp = p.Parent.HumanoidRootPart -- in this scenario i'll use the humanoid root part as an example. local magnitude = hrp.Velocity.Magnitude -- this will return the players walkspeed. if magnitude > 16 then print("players walkspeed is over 16.") db = false else db = false end end end end)
Learn more about magnitude here.
Here's the script you were asking for. Put it inside of a part. There are many ways to tweak this, like putting names into a module and using Region3s to reduce the amount of objects, but this should work perfectly fine for now. Good luck on your game and learning scripting!
local scan = script.Parent local eligblenames = {"Chassis"} -- your table to store car names. local speedlimit = 20 local debounce = false scan.Touched:Connect(function(p) if table.find(eligblenames, p.Parent.Name) then -- using table.find to check if the name of the parts parent exists in the table, returns true if the name matches any of the names in the table. if debounce == false then debounce = true local magnitude = p.Velocity.Magnitude -- getting the magnitude if magnitude >= speedlimit then --distribute ticket to the player print('ticked distributed') wait(20) -- wait time before distrubuting another ticket, makes sure they wont get flooded by any debounce = false else debounce = false end end end end)