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

How can I create a script that allows a part to give me a gear?

Asked by 3 years ago

Basically I want a script that gives you a gear from Lighting when you touch a certain part, then removes that part you just touched.

The gear in Lighting and part in Workspace also have a StringValue inside of them.

The value inside of the gear in Lighting is blank, as I want it to copy the value from the part in the Workspace.

I can somewhat get the script to work when it is inside of the part, but not at all when the script is in the Workspace. The most it has done is give me the item..

Current Script: (Inside of Workspace)

function touch(hit)
    local item2 = game.Lighting.Storage.MountainDew
    local debounce = false
    script.Parent.Touched:connect(function(part) 
        if hit.Name == game.Workspace:FindFirstChild("MountainDew") ~= true then
            local player = game.Players:GetPlayerFromCharacter(part.Parent)
            if player ~= nil then
                if player.Backpack ~= nil then
                    if debounce == false then
                        debounce = true
                        item2:Clone().Parent = player.Backpack

                        hit.Name = "To: " .. (game.MountainDew.House.Value)
                        hit.House.Value = game.MountainDew.House.Value
                        game.Workspace.MountainDew:remove()

                        wait(3)
                        debounce = false
                    end
                end
            end
        end
    end)
end
script.Parent.Touched:connect(touch)

Im so confused on why it's not working. If anyone could help, that would be great.

3 answers

Log in to vote
0
Answered by
iiii676 23
3 years ago

Hey I have a super simple model on this and I'll send you the script I used, It requires putting the tool in ReplicatedStorage, you can edit the script around.

Heres the link to the model: https://www.roblox.com/library/5349028096/Easily-Customisable-Giver

Heres the script:

local Debounce = false

script.Parent.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent)and Debounce == false then
        local Tool = game.ReplicatedStorage:FindFirstChildOfClass("Tool")
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local NewTool = Tool:Clone()
        NewTool.Parent = Player.Backpack
        Debounce = true
        wait(3)
        Debounce = false
    end
end)

Where I put FindFirstChildOfClass("Tool"), You can put your tool name.

0
Hey. Thank you man. :) Nath390Fish 19 — 3y
0
Hey. Thank you man. :) It worked perfectly. I also added "script.Parent:Remove()", which removes the part just like I wanted. Nath390Fish 19 — 3y
Ad
Log in to vote
0
Answered by
emervise 123
3 years ago
Edited 3 years ago

Put the tool in ServerStorage

-- define variables

local touchPart = game.Workspace.part
local tool = game.ServerStorage.MountianDew
local toolClone = tool:Clone()
local cooldown = false

-- execute event and function

touchPart.touched:Connect(function(partTouched)
    if cooldown == true then
        print("cooldown")
    else
        local char = partTouched.Parent
        toolClone.Parent = char
        cooldown = true
        wait(5)
        cooldown = false
    end
end)
0
I made a mistake in my code, I'm editing it emervise 123 — 3y
0
done emervise 123 — 3y
0
Hey. I tried this and for some reason it doesnt work. I have no clue why, Seems like it would work... Nath390Fish 19 — 3y
0
I checked over everything, such as the typo for Mountain, and it still doesnt work.. I think it has something to do with "char" Does roblox automatically know what "Char" is? Nath390Fish 19 — 3y
View all comments (3 more)
0
i defined it on line 14- emervise 123 — 3y
0
Also, it worked for me. emervise 123 — 3y
0
Make sure the touch part is named "part" and the tool is in server storage emervise 123 — 3y
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The "MountainDew" tool needs to be placed in game.Lighting. The script below needs to be put in the part. I have also added comments to explain the script

Deb = false --Debounce (In seconds)

script.Parent.Touched:Connect(function(Hit) --on hit
    if Hit.Parent:FindFirstChild("Humanoid") and Deb == false then --checks if it's a player and if debounce is false
        Deb = true --if it's false and humanoid exists, then it puts Deb to true
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) --gets the player from the character (The character is in game.Workspace)
        game.Lighting.MountainDew:Clone().Parent = Player.Backpack --clones the mountain dew in lighting and puts it in the player's backpack
        wait(3)--wait 3 seconds
        Deb = false --puts Debounce to false
    end
end)

Answer this question