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

Why does this script do nothing, even tho its supposed to do something? no errors

Asked by 5 years ago
Edited 5 years ago

I made a spawner, but when i click G, it wont do anything.

first script (at the startergui):

local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local name = plr.Name..("'s Car")
local debounce = false
uis.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.G then
for i,v in pairs(game.Players.LocalPlayer.Character:GetChildren())do
    if v:IsA("Model") and v.Name == name then
    v:Destroy()

    end
    end
    end
end)
uis.InputEnded:Connect(function(aey)
wait()
game.ReplicatedStorage.SpawnCarEvent:FireServer()
end)

second script(at the workspace):

local debounce = true
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(0.1)
        debounce = false
            end
            end
        end)
end)

thanks for helping!

0
You spelt "local" wrong. You typed "llocal". User#24093 0 — 5y
0
Add print statementsto parts of your code, so you know which function is running and which functions is not, this will help narrow the problem down, also your first line, you spelled local wrong aazkao 787 — 5y
0
Oh, it actually wasnt misspelled,i misspelled it in the site lmao. ieatandisbaconhair 77 — 5y

1 answer

Log in to vote
0
Answered by
aazkao 787 Moderation Voter
5 years ago
Edited 5 years ago

Your second script is where the problem is at

"not" will reverse the boolean value , your debounce at the beggining is true, so when you put if not debounce then it means if false then, which of course will not run as the value is false

Also you UserInputService doesnt work in server scripts

local debounce = true

game.ReplicatedStorage.SpawnCarEvent.OnServerEvent:Connect(function(plr)


            if 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(0.1)
        debounce = false

            end

end)
0
Then how do i user UserInputServicefor server scripts? ieatandisbaconhair 77 — 5y
0
You dont, its impossible, and you do not need to use userinputservice in your server script here anyway as the event will only fire when a user pressess G as seen in your local script aazkao 787 — 5y
0
EDIT: move your fireServer to your inputbegan codes, right now FireServer() will be invoked whenever a player does anything on their keyboard/mouse aazkao 787 — 5y
0
and its because you put it into your input ended event without putting any if statements to check that the key pressed is G aazkao 787 — 5y
View all comments (2 more)
0
actually, it did work before, but it spammed then i added debounce. ieatandisbaconhair 77 — 5y
0
yes i already said in my answer that your debouncing is where it went wrong and corrected it for you, im just saying that if you put your FireServer where it is it will always trigger whenever a user does anything, as you didnt add a checking to make sure that the button they press is G aazkao 787 — 5y
Ad

Answer this question