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.
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.
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)
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.
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?
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.
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)