How to make a block that gives you a tool

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

If the player jumps, TouchEnded fires.

1 Like

Then OP could either let the player not jump when on the button? But that already defeats the purpose of what hes trying to do

He could, but in my opinion I think that using the value method is better.

Thanks for all the help! I will use the bool value.

If you need any further help, please shoot me a private message.

I just found this. @True_Snowi perhaps this can help you. It’s not marked as solved as of writing but I bet the replies already existing can help you.

Alright this is simple script I just made to give it ONCE.
(server)

    script.Parent.Touched:Connect(function(hit)
    local player = game.Players:FindFirstChild(hit.Parent.Name)
    local itemname = "TOOL NAME HERE"
    local tool = game.ServerStorage:FindFirstChild(itemname)
    if player then
    if player.Backpack:FindFirstChild(itemname) then
    print("player has the" .. itemname .." tool already")
    else
    warn("giving ".. toolname .." to ".. player.Name.." backpack's")
    tool:Clone().Parent = player.Backpack
    end
    end
    end)

This next script is when they leave the sword area.
(server)

script.Parent.Touched:Connect(function(hit)
local player = game.Players:FindFirstChild(hit.Parent.Name)
local itemname = "TOOL NAME HERE"
  if player then
    if player.Backpack:FindFirstChild(itemname) then
    print("player has the" .. itemname .." tool, deleteing")
      player.BackPack:FindFirstChild(toolname):Destroy()
    else
    print(player.Name .."Does not have a tool oddly :/")
    end
    end
end)

ps I’m not in studio I could of made a script erorr.

10 Likes

The only errors i’ve spotted is the capital P at line 6 of the second script which is supposed to be lowercase and “toolname” which is supposed to be itemname. Other than that your script works good, you rock!