Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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!

3 comments

0

You spelt "local" wrong. You typed "llocal".

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

0

Oh, it actually wasnt misspelled,i misspelled it in the site lmao.

1 answer

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)

6 comments

0

Then how do i user UserInputServicefor server scripts?

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

0

EDIT: move your fireServer to your inputbegan codes, right now FireServer() will be invoked whenever a player does anything on their keyboard/mouse

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

0

actually, it did work before, but it spammed then i added debounce.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.