Greetings everyone,
I’ve created a temporary system for vehicle spawning which seems to work fine from the outside, but has some bugs inside. Players can only spawn in designated checkpoints.
Now, I know touched events or part-based hitboxes are pretty bad for both detection and performance, but I wanted to rush the game. Now since my game is starting to grow, I’m forced to fix these bugs.
Anyways, when a player presses the spawn button outside, it doesn’t spawn anything.
However, as soon as a player enters a checkpoint, it despawns any current one(s) and spawns another one.
Here’s the code:
Server-Side:
local replicatedstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
replicatedstorage.SpawnTukTuk.OnServerEvent:Connect(function(player)
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local humanoid = char.Humanoid
if humanoid.Sit == true then return end
for _, part in pairs(workspace:GetChildren()) do
if part.Name == "TukTuk" or "GoldenTukTuk" then
if part:GetAttribute("Owner") == player.Name then
part:Destroy()
end
end
end
local touchconnect
touchconnect = humanoid.Touched:Connect(function(hitpart)
if hitpart.Parent == workspace.Checkpoints then
local tuktuk = replicatedstorage.TukTuk:Clone()
tuktuk:SetAttribute("Owner", player.Name)
tuktuk:PivotTo(hrp.CFrame*CFrame.new(0,0,-5)*CFrame.Angles(-67.5,0,0))
tuktuk.Parent = workspace
touchconnect:Disconnect()
end
end)
end)
workspace.Checkpoints["5"].Touched:Connect(function(hitpart)
local char = hitpart.Parent
local hum = char:WaitForChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(char)
if plr then
replicatedstorage.RebirthScreen:FireClient(plr)
end
end)
replicatedstorage.SpawnGoldenTukTuk.OnServerEvent:Connect(function(player)
local char = player.Character
local hrp = char:WaitForChild("HumanoidRootPart")
local humanoid = char.Humanoid
for _, part in pairs(workspace:GetChildren()) do
if part.Name == "TukTuk" or "GoldenTukTuk" then
if part:GetAttribute("Owner") == player.Name then
part:Destroy()
end
end
end
local touchconnect
touchconnect = humanoid.Touched:Connect(function(hitpart)
if hitpart.Parent == workspace.Checkpoints then
local tuktuk = replicatedstorage.GoldenTukTuk:Clone()
tuktuk:SetAttribute("Owner", player.Name)
tuktuk:PivotTo(hrp.CFrame*CFrame.new(0,0,-5)*CFrame.Angles(-67.5,0,0))
tuktuk.Parent = workspace
touchconnect:Disconnect()
end
end)
end)
players.PlayerRemoving:Connect(function(player)
for _, part in pairs(workspace:GetChildren()) do
if part.Name == "TukTuk" or "GoldenTukTuk" then
if part:GetAttribute("Owner") == player.Name then
part:Destroy()
end
end
end
end)
Client-Side:
spawnbutton.MouseButton1Click:Connect(function()
if hum.Sit == false then
click2:Play()
screengui.Parent.ArrowGui.Arrow.Visible = false
replicatedstorage.SpawnTukTuk:FireServer()
end
end)
I know, this is terribly optimised, but it does get the job sort of done. I’m open to any Improvements
Another thing,
Since I detect spawning by parts, whenever a low-end user drives a vehicle, it causes immense lag inside the spawning box, but as soon as they go outside of the box, the lag fades away. Any help on this is very appreciated!