I want to make it so when you touch a part, you get a sword and when you stop touching the part, the sword gets removed from ur inventory
when i touch the part it puts the sword in my inventory and then right after its deletes it right away
i have looked everywhere and i cannot make it work
local item = game:GetService("ReplicatedStorage"):WaitForChild("ClassicSword")
local db = true
script.Parent.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") ~= nil then
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if player ~= nil then
if player.Backpack ~= nil then
if db == true then
db = false
item:Clone().Parent = player.Backpack
wait(1)
db = true
end
end
end
end
end)
script.Parent.TouchEnded:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") ~= nil then
local player = game.Players:GetPlayerFromCharacter(part.Parent)
for i, v in pairs(player.Backpack:GetChildren()) do
v:Destroy()
end
end
end)
So what I would try for this is have a Bool Value stored in the player that is set to true in upon touching the sword part, and if the player is not touching the sword part then the Bool Value would be set to false. In another script (perhaps serverscriptservice) keep checking whether or not the value is true. If it is then sword is put in, if it isn’t sword is destroyed.
The Issue here is that the TouchEnded event is firing anytime anything stops touching the part (like your left and right foot for example). What you need to do is to distinguish the TouchEnded event between players and different parts. One way to fix this is to weld an invisible part that is noncollide to the player, then detect when that part starts or ends touch, and ignore every other touch
EDIT: I went ahead and refined the code to make it work better. All you need to do is create a part named “HitDetection” (you can rename it to whatever as long as you change it in the script). Then you just need to parent it and weld it to the player, make it non-collide, and make it transparent. And make sure the part is the size that you want the “hit box” to be. Now when you touch and end touch it should work seamlessly
local item = game:GetService("ReplicatedStorage"):WaitForChild("ClassicSword")
local db = true
script.Parent.Touched:Connect(function(part)
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if part.Name == "HitDetection" then
item:Clone().Parent = player.Backpack
end
end)
script.Parent.TouchEnded:Connect(function(part)
if part.Name == "HitDetection" then
local player = game.Players:GetPlayerFromCharacter(part.Parent)
for i, v in pairs(player.Backpack:GetChildren()) do
v:Destroy()
end
end
end)
If you don’t mind using open source modules, Zone+ is a pretty good module for this use case. And AFAIK, .Touched and .TouchEnded is very unreliable on the server. You could detect .Touched and .TouchEnded on the client, then fire a remote event to tell the server that you entered/left the part. Of course you need sanity checks on the server so players won’t abuse the remote event.