I am trying to create a realistic driving game and I wanted to implement a driving challenge and if the player crashes or bumps into something it will take a certain amount of points away from them.
Currently, I have tried many methods and non of them seem to work so I’ve decided to post here.
Here is the script I’m using that detects if the vehicle hits something
script.Parent.Touched:Connect(function(hitturn)
if hitturn.Name == "Object" then
print("You crashed")
wait(1)
end
end)
This code is just placed into the body mesh of the car so whenever the actual body touches something it’s detected.
If you need anymore info please let me know!
Thank you.
Feel free to download this test version: Test2.rbxl (101.8 KB)
--//Services
local Players = game:GetService("Players")
--//Functions
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local score = Instance.new("NumberValue")
score.Name = "Points"
score.Value = 2
score.Parent = leaderstats
end)
Replace your detect script with this:
--//Services
local Players = game:GetService("Players")
--//Variables
local Detect = script.Parent
local Vehicle = Detect.Parent.Parent
local Debounce = false
--//Functions
Detect.Touched:Connect(function(hitpart)
if hitpart.Name == "Object" and not Debounce then
Debounce = true
local Player = Players:GetPlayerFromCharacter(Vehicle.DriveSeat.Occupant.Parent)
if Player and Player.leaderstats.Points.Value > 0 then
Player.leaderstats.Points.Value -= 1 -- amount of points taken away
end
task.wait(1)
Debounce = false
end
end)