Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Speed detector for a car?

Asked by 1 year ago
Edited 1 year ago

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)

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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)

0
Hey, The script doesn't work for players that are inside the car and for players that are normally walking over 16. BawkBawkBattles 2 — 1y
0
It worked fine for me, and for the cars, I recommend instead of trying to get the player inside of the car, you can get the magnitude of the car or any part of the car. plooring 96 — 1y
0
How can i do that? Im new to scripting, so most of the times stuff makes almost no sense to me, haha ;') BawkBawkBattles 2 — 1y
0
You could do something similar to what I did. Use a Part Touched event and check if the part that touched it is a child of a car. This could be done through many ways like getting the name, having a indicator inside the car, etc. Once you've detected it is a car get the magnitude of any part or the vehicle seat by doing [part name].Velocity.Magnitude. If the magnitude is over a specific amount (1) plooring 96 — 1y
View all comments (7 more)
0
, they would get a speeding ticket. You could also learn about Region3s and set up specific regions in which a player could get a ticket for speeding in specific areas, but I'm getting side tracked. plooring 96 — 1y
0
I'll see if I can make a script for you to look at. plooring 96 — 1y
0
Hmm, im not really understanding most of this sadly.. Is there any way you could possibly create another script for me? I'm new to the scripting, and i can make more "advanced" scripts with the help of youtube tutorials. + There is almost 0 videos on how to do a speed detector for a car. It would really mean a lot to me if i got it working, since its the part of the game im working on. BawkBawkBattles 2 — 1y
0
Please see my updated post. plooring 96 — 1y
0
Hey, so the "local eligblenames = {"Chassis"} -- your table to store car names." Is the Chassis part a folder, since its not the same name for me? BawkBawkBattles 2 — 1y
0
No, its a table. You can put the name of the car in there, separated by a comma, for example, “local eligiblecars = {“Chassis”, “Ford F150”}”. I recommend learning about tables. plooring 96 — 1y
0
Works, ty :) <3 BawkBawkBattles 2 — 1y
Ad

Answer this question