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

Is there any way to make this function activate when I press a key?

Asked by 7 years ago
Edited 7 years ago

What I am trying to do with my script is to make an "magic circle" expand when I press "Q" This is what I have V

function ActivateQ(Decal)
game.Workspace.FollowingBrick.Size = Vector3.new(2,.1,2)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(2.5,.1,2.5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(3,.1,3)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(3.5,.1,3.5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(4,.1,4)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(4.5,.1,4.5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(5,.1,5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(5.5,.1,5.5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(6,.1,6)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(6.5,.1,6.5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(7,.1,7)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(7.5,.1,7.5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(8,.1,8)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(8.5,.1,8.5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(9,.1,9)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(9.5,.1,9.5)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(10,.1,10)
wait(.1)
game.Workspace.FollowingBrick.Size = Vector3.new(2,.1,2)
end

But I can't figure out how to make it activate when I press "Q"

Edit: Is there any way to make the script shorter?

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You can use a numerical for loop to shorten your script - also, using variables helps keep your code clean and concise.

function grow_shrink(o) --'o' will be the object
    for i = 2, 10, .5 do --define for loop
        --set size according to current iteration
        o.Size = Vector3.new(i,.1,i)
        wait(.1)
    end
    wait(.1)
    o.Size = Vector3.new(2,.1,2) --reset size
end

To make this occur upon pressing Q, you're going to have to use remotes.

Use the InputBegan function of UserInputService in a LocalScript on the client to call the FireServer function on your RemoteEvent.

local uis = game:GetService("UserInputService")
local re = game.ReplicatedStorage.Magic --This is your RemoteEvent

uis.InputBegan:Connect(function(i,p)
    if not p then --Make sure client isn't typing
        if i.KeyCode == Enum.KeyCode.Q --If key was q
            re:FireServer() --Tell server to execute code
        end
    end
end)

Now, you can setup a script in ServerScriptStorage to retrieve the FireServer call using the OnServerEvent event:

local re = game.ReplicatedStorage.Magic --This is your RemoteEvent

function grow_shrink(o)
    for i = 2, 10, .5 do
        o.Size = Vector3.new(i,.1,i)
        wait(.1)
    end
    wait(.1)
    o.Size = Vector3.new(2,.1,2)
end

re.OnServerEvent:Connect(function() --OnServerEvent event
    grow_shrink(workspace.FollowingBrick) --call the function
end)
0
oh, thanks, this helped a lot. But one question, can you explain how the for loop works? SmugNyan 24 — 7y
0
You give it 3 numbers - start,finish,increment. For loop will "iterate" from start to finish, in increments of the increment or 1 Goulstem 8144 — 7y
0
I put a hyperlink there :) Goulstem 8144 — 7y
Ad

Answer this question