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

Scripting a Drink Dispenser?

Asked by 2 years ago

Hey! I know absolutely nothing about scripting and I really wanted to learn how to script a drink dispenser. I already have the cup giver and the machines model. I now need the script. Can someone teach me please? I don't even know where to put the scripts let alone writing them.

1 answer

Log in to vote
0
Answered by
ShaShxa 105
2 years ago

Hey!

If you are completely new to scripting and would like to start, I'd recommend watching this playlist by AlvinBlox to get started.

Here, I will try teaching you how to give someone a drink once they have touched a part.

This will only cover on how to give the player an item!

First of all, we need to create a script. you can create a script under the workspace.

Now, we will create a new part of our workspace with our script. We will be using variables and the function Instance.new(). This will be the part we will use in the future to give a drink.

local Part = Instance.new("Part") -- Creating the new part.

We have now created a new part, but it isn't appearing yet. For that, we will be editing its properties.

Every part has its properties. Colors, Transparency, Location, Parent, and more. We will now set the Parent as the workspace, so the part will appear in the workspace.

local Part = Instance.new("Part") -- Creating the new part.
Part.Parent = workspace -- Sets the parent as workspace, if you would like to know more about it click on the blue parent word.

We have now set the parent as "workspace", which means we will see the part appearing. A nice touch to add is to anchor it, which will prevent it from moving when people touch it. We can simply change the property for that again.

local Part = Instance.new("Part") -- Creating the new part.
Part.Parent = workspace -- Sets the parent as workspace, if you would like to know more about it click on the blue parent word.
Part.Anchored = true -- Makes the part anchored.

Great! We have now created a part with our script which we will use in the future. If you do not see the part appearing, try deleting the "SpawnLocation" in the workspace as we didn't define its position.

Now what we want to do is make the script respond every time a player touches it. For that, we will use the Event Touched

But before, let's make a function

local Part = Instance.new("Part") -- Creating the new part.
Part.Parent = workspace -- Sets the parent as workspace, if you would like to know more about it click on the blue parent word.
Part.Anchored = true -- Makes the part anchored.

local function TouchedPart() -- Functions are sets of instructions that can be used multiple times in a script. Once defined, a function can be executed through a command or triggered through an event.
    print("Hello!") -- every time the function is executed, it will print "Hello!" in the output/console.
end

We have now created a function that lets us bind the Touched Event to the function.

local Part = Instance.new("Part") -- Creating the new part.
Part.Parent = workspace -- Sets the parent as workspace, if you would like to know more about it click on the blue parent word.
Part.Anchored = true -- Makes the part anchored.

local function TouchedPart() -- Functions are sets of instructions that can be used multiple times in a script. Once defined, a function can be executed through a command or triggered through an event.
    print("Hello!") -- every time the function is executed, it will print "Hello!" in the output/console.
end

Part.Touched:Connect(TouchedPart) -- every time the Part will be touched, the function "TouchedPart()" will be executed.

Great! Every time we touch the part we will see "Hello!" in the console/output.

Now, how do we give the player a drink? First of all, put the drink inside the script. You have to do that through Roblox studio. Rename the drink/cup to "Drink" so our script can use it. We will now tell the script where the Drink is located at.

local Part = Instance.new("Part") -- Creating the new part.
Part.Parent = workspace -- Sets the parent as workspace, if you would like to know more about it click on the blue parent word.
Part.Anchored = true -- Makes the part anchored.

local Drink = script.Drink -- This will tell our script that we have put a drink inside of the script children*.
-- "children" means all Parts that are inside of another Part.
-- (I hope this is not too difficult to understand)

local function TouchedPart() -- Functions are sets of instructions that can be used multiple times in a script. Once defined, a function can be executed through a command or triggered through an event.
    print("Hello!") -- every time the function is executed, it will print "Hello!" in the output/console.
end

Part.Touched:Connect(TouchedPart) -- every time the Part will be touched, the function "TouchedPart()" will be executed.

Now that our script knows where the drink is, we can give it to the player every time it touches the part. We will be using Clone() and FindFirstChild()for this. We will also add a variable to define which player has touched the event.

local Part = Instance.new("Part") -- Creating the new part.
Part.Parent = workspace -- Sets the parent as workspace, if you would like to know more about it click on the blue parent word.
Part.Anchored = true -- Makes the part anchored.

local Drink = script.Drink

local function TouchedPart(WhatTouchedIt) -- Functions are sets of instructions that can be used multiple times in a script. Once defined, a function can be executed through a command or triggered through an event.
    local ClonedDrink = Drink:Clone() -- Will duplicate/clone the drink, so we can give it to the player.

    -- We have added a variable inside of "TouchedPart()", which lets us see which part has touched it.

    local CharacterPlayer = WhatTouchedIt.Parent -- Whenever we touch a part, it will check which body part has touched it. We do not want that so we add a ".Parent" to go to the player's character.
    local ActualPlayer = game.Players:FindFirstChild(CharacterPlayer.Name) -- We will try to find the actual player in the player list based on the character!

    if ActualPlayer then -- Check if the script found the player
        ClonedDrink.Parent =  ActualPlayer:WaitForChild("Backpack") -- Put the drink/cup inside of the player's backpack. We added :WaitForChild() which means it will wait until it has found something we are looking for, in this case, "Backpack".
    end
end

Part.Touched:Connect(TouchedPart) -- every time the Part will be touched, the function "TouchedPart()" will be executed.

Cool! We have now made it so that every time the player touches the part, it will receive a drink! But we have to prevent it from giving it too much! We will do that by adding a cooldown. We will use debounce for that.

local Part = Instance.new("Part") -- Creating the new part.
Part.Parent = workspace -- Sets the parent as workspace, if you would like to know more about it click on the blue parent word.
Part.Anchored = true -- Makes the part anchored.

local Debounce = false -- Add a cooldown variable

local Drink = script.Drink

local function TouchedPart(WhatTouchedIt) -- Functions are sets of instructions that can be used multiple times in a script. Once defined, a function can be executed through a command or triggered through an event.
    local ClonedDrink = Drink:Clone() -- Will duplicate/clone the drink, so we can give it to the player.

    -- We have added a variable inside of "TouchedPart()", which lets us see which part has touched it.

    local CharacterPlayer = WhatTouchedIt.Parent -- Whenever we touch a part, it will check which body part has touched it. We do not want that so we add a ".Parent" to go to the player's character.
    local ActualPlayer = game.Players:FindFirstChild(CharacterPlayer.Name) -- We will try to find the actual player in the player list based on the character!

    if CharacterPlayer then -- Check if the script found something
        if Debounce == false then -- If the cooldown is not active, continue.
            Debounce = true -- Set the cooldown to true, we now have a cooldown!
            ClonedDrink.Parent = ActualPlayer:WaitForChild("Backpack") -- Put the drink/cup inside of the player's backpack. We added :WaitForChild() which basically means it will wait until it has found "Backpack"
            wait(1) -- Waits one second before the code continues.
            Debounce = false -- Set the cooldown to false, the user can now get a drink again!
        end
    end
end

Part.Touched:Connect(TouchedPart) -- every time the Part will be touched, the function "TouchedPart()" will be executed.

Good! We have now successfully created our dispenser script. Please note that it can be difficult to understand with no scripting experience. (As I have rushed this a bit.) Take your time and search for specific stuff you do not understand. I hope I have helped you! I'm not the best at explaining.

Ad

Answer this question