#Summoning parts while 'C' is being held

41 messages · Page 1 of 1 (latest)

burnt marsh
#

So I've got this code that works really fine; when the C is pressed a part (lol) is created.
I would like to make so that if the key C is clicked the part is going to be created until you unpress the key C
(I'm a beginner)

spice pumice
#

code looks good just create a boolean variable such as

#

holding = false

#

then set it to true as the player holds it

#

use t he function userinput.InputEnded to detect when they let go of the key and set holding to false

#

if u want to create parts while the key is held u would use while holding == true do

burnt marsh
#

I don't really understand it
let's say we've got the "holding = false" boolean variable but how does the script know when we hold it?

burnt marsh
spice pumice
burnt marsh
#

I know

verbal inletBOT
#

studio** You are now Level 2! **studio

spice pumice
#

so do u want ur script to spawn blocks over and over while u hold c

#

and then when u release it stops

burnt marsh
#

yeah

#

exactly

spice pumice
#

alr can u send script in txt format ill rearrange it

#

u will see what i mean

burnt marsh
#

local lol
local userinput = game:GetService("UserInputService")

local function pressedC(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.C then
lol = Instance.new("Part")
lol.Parent = game.Workspace
lol.Position = Vector3.new(10, 100, 10)
lol.Shape = Enum.PartType.Block
lol.Size = Vector3.new(5, 5, 5)
wait(1)
end
end
end

userinput.InputBegan:Connect(pressedC)

spice pumice
#
local lol
local userinput = game:GetService("UserInputService")
local holding = false

local function pressedC(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.C then
            holding = true
            while holding == true do
                
            
            lol = Instance.new("Part")
            lol.Parent = game.Workspace
            lol.Position = Vector3.new(10, 100, 10)
            lol.Shape = Enum.PartType.Block
            lol.Size = Vector3.new(5, 5, 5)
            wait(1)
            end
        end
    end
end

userinput.InputBegan:Connect(pressedC)


userinput.InputEnded:Connect(function(Input, istyping)
    if Input.KeyCode == Enum.KeyCode.C then
        holding = false
    end
end)
#

should work nicely now

#

so u would just run a check and say while holding == true do

#

then write t he part spawning code

#

and then detect when C stops being held and change holding to false so the loop stops

#

if u get me

burnt marsh
#

ohh I get it now

spice pumice
#

its good to use things like that

#

boolean values are great and dont require much extra scripting

burnt marsh
#

well I was actually thinking to set the "pressedC" to nil or something like that

spice pumice
#

its kinda like a toggle

burnt marsh
#

but I didn't know how

burnt marsh
burnt marsh
#

I thought that to stop the pressedC function I had to set it to false/nil

#

but I guess I was wrong

spice pumice
#

u cant set properties of functions

verbal inletBOT
#

studio** You are now Level 2! **studio

burnt marsh
#

I realized that as I tried it

spice pumice
#

well im glad u learned something and g ood luck with ur game

burnt marsh
#

well it's not a game, but I'm just practicing

#

thanks a lot man!