Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

2 comments

0

Hey. Thank you man. :)

0

Hey. Thank you man. :) It worked perfectly. I also added "script.Parent:Remove()", which removes the part just like I wanted.

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)

7 comments

0

I made a mistake in my code, I'm editing it

0

done

0

Hey. I tried this and for some reason it doesnt work. I have no clue why, Seems like it would work...

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?

0

i defined it on line 14-

0

Also, it worked for me.

0

Make sure the touch part is named "part" and the tool is in server storage

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.