-- StuntServer
-- place in ServerScriptService
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local trickEvent = ReplicatedStorage:FindFirstChild("ScooterTrick")
if not trickEvent then
trickEvent = Instance.new("RemoteEvent")
trickEvent.Name = "ScooterTrick"
trickEvent.Parent = ReplicatedStorage
end
local TRICK_FAIL_CHANCE = 0.5
local COOLDOWN = 0.8
local lastTrick = {}
local allowedTricks = {
Barspin = true,
Tailwhip = true,
Backflip = true,
Frontflip = true,
}
local function getScooter(player)
local character = player.Character
if not character then
return nil
end
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
return nil
end
local seat = humanoid.SeatPart
if not seat or not seat:IsA("VehicleSeat") then
return nil
end
local scooter = seat:FindFirstAncestorOfClass("Model")
if not scooter then
return nil
end
if seat:GetAttribute("IsScooter") ~= true then
return nil
end
return scooter, seat, character, humanoid
end
trickEvent.OnServerEvent:Connect(function(player, action)
if typeof(action) ~= "string" then
return
end
if not allowedTricks[action] then
return
end
local now = os.clock()
if lastTrick[player] and now - lastTrick[player] < COOLDOWN then
return
end
local scooter, seat, character, humanoid = getScooter(player)
if not scooter then
return
end
lastTrick[player] = now
-- 50% chance of a failed stunt.
if math.random() < TRICK_FAIL_CHANCE then
trickEvent:FireClient(player, "Bail", action)
task.delay(0.15, function()
if not character.Parent then
return
end
if humanoid.Health <= 0 then
return
end
humanoid.Sit = false
humanoid:ChangeState(Enum.HumanoidStateType.Freefall)
end)
return
end
-- Tell the rider that the stunt was accepted.
trickEvent:FireClient(player, "Success", action)
end)
Players.PlayerRemoving:Connect(function(player)
lastTrick[player] = nil
end)