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

How can I merge these two scripts into one function for a click detector block?

Asked by 9 years ago

Basically i'm trying to make brick that when clicked when clicked, moves a certain model from the serverstorage into the workspace, waits for a little bit, then enables a certain script and then when the block is clicked again it does the reverse moves said model from the workspace back into the serverstorage from the worskpace, waits for a little bit then disables said script. Thanks to viral moose and Scriptree i've learnt how to do these specific functions seperately but I seem to be having alot of trouble joining them together under one script :c

Here are the two scripts (note:They need to both function under one clickdetector brick for what I'm trying to create out of this. I know one's for a gui button and another is for a clickdetector brick

local script1 = workspace.script1 -- Put location of your script you want to enable/disable
function onButtonClicked()
script1.Disabled = false
end
script.Parent.MouseButton1Down:connect(onButtonClicked)

^credit to viralmoose for providing and

local serverstorage = game:GetService("ServerStorage") -- serverstorage is the service to store whatever you want in, Lighting was used before ServerStorage was made

local model = serverstorage:WaitForChild("cheese") -- replace "cheese" with the name of the model under serverstorage
local brick = script.Parent -- variable for the parent of the script, which is what the clickdetector is going to be under
local detector = Instance.new("ClickDetector", brick) -- creating a clickdetector, which is used to be able to click a specific part, and parenting it to the script's parent.

local isinwork = false -- creating debounce for later usage

--[[You have to already have the model positioned as you want it to be shown as, in serverstorage, when it's parented to workspace it will be shown in the position you had it at when u stored the model in serverstorage]]--

function moveModel() -- function that will be called when someone clicks the part
    if not isinwork then  -- checking if the debounce is false, if it is then it will parent it to workspace
        model.Parent = game.Workspace
        isinwork = true -- here it is setting the debounce to true so when it's clicked again it is parented to serverstorage
    else -- if the debounce is true then it will parent it to serverstorage
        model.Parent = serverstorage
        isinwork = false -- here it is setting the debounce to false so when it's clicked again it is parented to workspace
    end
end

detector.MouseClick:connect(moveModel) -- calling the function when the brick has been clicked

^credit to Scriptree for providing.

I'm not very experienced at all in making scripts from scratch on ROBLOX, I only know some of the basic terminology (but even then, I don't know which terminology are really deemed as "basic" so you can see my struggle here.)

Help would be very much appreciated and I'll be sure to credit you properly if you can help me find a working solution. :D

1 answer

Log in to vote
0
Answered by 9 years ago

First of all, don't use other peoples scripts(unless its for learning purposes of course!), no one ever made a good game without making their own scripts and learning, plenty of resources out there, start at (wiki.roblox.com).

[[QUICK EXPLANATION]] Basically I combined the two functions and had one brick call that big function, let me know if I need to elaborate more of this doesn't work.


--ORIGINAL FUNCTION 1

local script1 = workspace.script1 -- Put location of your script you want to enable/disable
function onButtonClicked()
script1.Disabled = false
end
script.Parent.MouseButton1Down:connect(onButtonClicked)

--ORIGINAL FUNCTION 2

local serverstorage = game:GetService("ServerStorage") -- serverstorage is the service to store whatever you want in, Lighting was used before ServerStorage was made

local model = serverstorage:WaitForChild("cheese") -- replace "cheese" with the name of the model under serverstorage
local brick = script.Parent -- variable for the parent of the script, which is what the clickdetector is going to be under
local detector = Instance.new("ClickDetector", brick) -- creating a clickdetector, which is used to be able to click a specific part, and parenting it to the script's parent.

local isinwork = false -- creating debounce for later usage

--[[You have to already have the model positioned as you want it to be shown as, in serverstorage, when it's parented to workspace it will be shown in the position you had it at when u stored the model in serverstorage]]--

function moveModel() -- function that will be called when someone clicks the part
    if not isinwork then  -- checking if the debounce is false, if it is then it will parent it to workspace
        model.Parent = game.Workspace
        isinwork = true -- here it is setting the debounce to true so when it's clicked again it is parented to serverstorage
    else -- if the debounce is true then it will parent it to serverstorage
        model.Parent = serverstorage
        isinwork = false -- here it is setting the debounce to false so when it's clicked again it is parented to workspace
    end
end

detector.MouseClick:connect(moveModel) -- calling the function when the brick has been clicked

--NEW SCRIPT

--First let's declare our variable from both scripts!

-------Variables
brick = game.Workspace.brick -- put the path to brick HERE
local serverstorage = game:GetService("ServerStorage")
local model = serverstorage:WaitForChild("cheese")
local detector = Instance.new("ClickDetector", brick) --THIS IS THE ONE CLICKABLE BRICK
local script1 = game.Workspace.script1

local isinwork = false --Debounce Variable

--Next we need our event, which you specified as the click on the clickdetector, we will just attach a big function to this that has both functions combined

detector.MouseClick:connect(function()

    if not isinwork then  -- checking if the debounce is false, if it is then it will parent it to workspace
            model.Parent = game.Workspace
            isinwork = true -- here it is setting the debounce to true so when it's clicked again it is         parented to serverstorage
    else -- if the debounce is true then it will parent it to serverstorage
            model.Parent = serverstorage
            isinwork = false -- here it is setting the debounce to false so when it's clicked again it is parented to workspace
    end -- UPDATE: FORGOT TO CLOSE THE IF STATEMENT SORRY :P!

    --END OF FIRST FUNCTION | BEGINNING OF NEXT FUNCTION

    script1.Disabled = false

end) -- Close the parenthesis
0
This seems like it should work but the click detector on the brick isn't showing up :( User#9115 0 — 9y
0
Update- it says there's an error on line 28 saying ')' expected (to close '(' at line 15) near '<eof>' User#9115 0 — 9y
0
O, sorry, forgot to put an end to close the if statement fixing right now! dragonkeeper467 453 — 9y
0
Yeah, that stopped the error but the click detector on the brick still isn't showing up (le cri) I've inputted the model and script names, do I also need to input the name of the brick that I want the click detector to go in somewhere? User#9115 0 — 9y
View all comments (6 more)
0
and  this script needs to be in the brick I want to be clickable for this function, right? User#9115 0 — 9y
0
O, you just need to define brick dragonkeeper467 453 — 9y
0
Ayyy, works now, is there a way to make it so when the model goes back to the serverstorage, the script becomes disabled again? User#9115 0 — 9y
0
yea just use an if statement, like if game.Workspace.model then --disable it dragonkeeper467 453 — 9y
0
Okay, whereabouts would I put this if statement? :) User#9115 0 — 9y
0
probably anywhere in the function dragonkeeper467 453 — 9y
Ad

Answer this question