So i made a spawner. If you click G, it spawn one car in front of your head. But it spawns too much, it spawns 4 of them. whats the problem?
script:
local debounce = false local uis = game:GetService("UserInputService") game.ReplicatedStorage.SpawnCarEvent.OnServerEvent:Connect(function(plr) uis.InputBegan:connect(function(aey) if aey.KeyCode == Enum.KeyCode.G then if not debounce then wait() local car = plr.SelectedCar.Value:Clone() car.Parent = game.Players.LocalPlayer.Character car.Chassis.Base.Anchored = true car.Name = plr.Name..("'s Car") car:SetPrimaryPartCFrame(plr.Character.Head.CFrame * CFrame.new(0,0,-10)) car:MakeJoints() car.Chassis.Base.Anchored = false debounce = true wait() debounce = false end end end) end)
Instance:FindFirstChild()
method to check if a car already exists. If it does, just don't spawn a car. You are also using LocalPlayer
and UserInputService
on the server, which of course will not work. Keep those on the client.local debounce = false game.ReplicatedStorage.SpawnCarEvent.OnServerEvent:Connect(function(plr) if workspace:FindFirstChild(plr.Name.."'s Car") then return end -- line above will check if there is an existing car. if so, end the function local car = plr.SelectedCar.Value:Clone() car.Chassis.Base.Anchored = true car.Name = plr.Name.."'s Car" car:SetPrimaryPartCFrame(plr.Character.Head.CFrame*CFrame.new(0,0,-10)) car.Parent = workspace car:MakeJoints() debounce = true wait(1) -- make it obvious! set 1 to which ever time lol debounce = false end)
LocalPlayer
, and handle user input and such.