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

If the value is true when the part is clicked, a function will run. ...Help?

Asked by 9 years ago
--If this is clicked
function onClicked()
    --Checks if at the right timing
    if Value then
        --Make part function
    game . Workspace : makepart() -- I put "game . Workspace" to prevent another error. Although I think it's going to break the script.
    end
  end
  --Connect the function.
  script.Parent.ClickDetector.MouseClick:connect(onClicked)
--Just a variable...
Value = true
-- ~Illuminati.~ The variable will change.
-- If true that means the timing of
--the person who clicked is correct.
while true do
    Value = false
    wait(0)
    Value = true
    wait(1)
end
--Make the part function.
local function makepart() --I'm wondering if these brackets are necessary.
    --The problem is that there's an error saying that I need to change this to local.
     local Make = Instance . new ("Part")
    Make . Parent = game . Workspace
    Make . Position = Vector3 . new (51, 30, 1)
end

2 answers

Log in to vote
1
Answered by
Diitto 230 Moderation Voter
9 years ago

1) The function makepart is not a member of workspace. You need to call it alone.

2) You need to wrap the while loop in a coroutine( or spawn or delay ), so that it lets the code under it run.

3a) Yes, you do need the "brackets"(they are not brackets, but parentheses), and don't localize the function, for then the onClicked function will not recognize the makepart function.

3b) Or, you could move the makepart function before the onClicked function and leave the while loop alone.

tl;dr answer:

local Value=true;
local makePart;makePart=function()
    local Make=Instance.new('Part',workspace);--// You don't need to state another line to set the parent of Make.
    Make.Position = Vector3.new(51,30,1);
end;

function onClicked()
    if Value then
        makePart()
    end;
end;

script.Parent.ClickDetector.MouseClick:connect(onClicked)

spawn(function()--// Starts the loop on another thread.
    while wait(1)do
        Value = false;
        wait(1)--// I had a feeling this was supposed to be 1.
        Value = true;
    end;
end);

Side Note: Why are you doing game . Workspace,, instead of game.Workspace? So much pointless whitespace.

0
`local makePart = function()` would be a much briefer way to say it; you should probably yourself include whitespace around things like `wait(1) do`... I also find semicolons after `end` quite off putting, they're also always unnecessary BlueTaslem 18071 — 9y
0
"[19/02/2015 16:33:37] Nolin: What's with your constant use of semicolons? This isn't C or Java." -> So? It helps to share a universal style for different languages. And the lack of whitespace is from the habit of code compression and obfuscation. Diitto 230 — 9y
Ad
Log in to vote
-2
Answered by 9 years ago

Why are you putting spaces?

Remove the spaces and test the code again.

0
This is a purely stylistic comment: *The spaces won't break the code,* and while they may not be standard practice, some people use them like that. (2-moth-old reply, shame on me) User#2 0 — 9y
0
No problem. Although why a downvote? -_- Darknesschaos 0 — 9y

Answer this question