How to make a block that gives you a tool

Hello, I am really new to scripting and I started making my first game, but the problem is I have no idea how to make this one script, I need a script where you can step on a part and it will give you a tool
and once you step off the part it takes the tool away.

Something like this.

https://gyazo.com/24bbf5f417b57c6bab4286c24a71111d

Should probably make an attempt first. With that in mind here are a few resources.

2 Likes

First you want your tool in, let’s just say ServerStorage. Then you want a regular script (not local) inside of the part. Then feel free to copy off this script.

local SS = game:GetService("ServerStorage")

script.Parent.Touched:Connect(function(hit)
local h = hit.Parent:findFirstChild("Humanoid")
if h ~= nil then
local player = game.Players:GetPlayerFromCharacter(h.Parent)
if player then
local tool = SS.--[[put the name of the tool here]]:Clone()
tool.Parent = player.Backpack
end
end
end)

Also, you should take a look at previous posts, as I am sure they have answered this already.
As @sjr04 said, you should probably make an attempt to do this first. After all, the best way to learn in my opinion is through trial and error. The sources provided are a good place to start.

14 Likes

I am also very new and If you find a solution Please let me know . I also what to learn how to do that (seems useful)

2 Likes

Okay thanks, I will make an attempt first!

1 Like

So I tired and got this https://gyazo.com/b31cdacea12d24d7a902a32f83e3b3be

I need it to only give the player 1 tool
and also need it for when they step off the mat the tools get taken away.

Go ahead and screenshot the script you have, as well as the output. The gyazo image you posted was only a couple of pixels and incredibly small.

So I used the script you gave me here.
(I tired changing some things around but no use)

and here is what happened.

I can’t read the output that well. It seems like an error has been encountered. Mind reading it aloud for me?

Sure, is says "Workspace.Script:2: Expected identifier when parsing expression, got ‘local’

Why not just use a debounce to make sure that it only gives you one tool?

Oh I know what I did wrong.

So, your new to scripting. Something us scripters use to prevent these types of things is called a debounce.

local SS = game:GetService("ServerStorage")
local debounce = false

script.Parent.Touched:Connect(function(hit)
local h = hit.Parent:findFirstChild("Humanoid")
if h ~= nil then
local player = game.Players:GetPlayerFromCharacter(h.Parent)
if player and debounce == false then
local tool = SS.--[[put the name of the tool here]]:Clone()
tool.Parent = player.Backpack
wait( --[[disired number here]])
debounce = false
end
end
end)

( @kylepo99 you beat me to it. But he is as mentioned just starting to begin to script)

3 Likes

Not sure if you meant to put debounce as false but i changed it to true and put a wait after player.backpack
and it worked, thanks!

but now I need a script for when you step off the mat it takes the tool away, so maybe I need to put a script into the floor?

I’d just put some walls around the area, and if a player touches those walls, remove the tool. Now if a player stays on the mat for longer than the debounce you put, they will get another sword, so you might want to integrate a bool value located inside the player/character on whether or not they are inside the area. It gets complex from there, but this should be a start.

I recommend watching @Alvin_Blox’s tutorials for beginners in this playlist to get used to scripting.

1 Like

Okay, will do. But I have found one more problem, so you go on the mat it gives you the tool, but if you die/reset and go back to the mat it doesn’t give the player anything, have a solution?

I think that’s because of the debounce.

Hmm, do you know how I could fix it?

  1. Shorten the debounce
  2. A bool value that is true if the player is on the mat, and false if the player is off. Touching the mat will turn the value true, touching those walls I was talking about a bit earlier would make the value false. The script will only give the tool if the value is set to false.

Either or.

Can’t you use a touchEnded function?

local SS = game:GetService("ServerStorage")
-- using @Eandvwigle script
local db = false 
script.Parent.Touched:Connect(function(hit)
if not db then
db = true
local h = hit.Parent:findFirstChild("Humanoid")
if h ~= nil then
local player = game.Players:GetPlayerFromCharacter(h.Parent)
if player then
local tool = SS.--[[put the name of the tool here]]:Clone()
db = false
tool.Parent = player.Backpack

script.Parent.TouchEnded:Connect(function()
player.Backpack.ToolName:Destroy()
end
end
end)
end)
2 Likes