Need help with a part giving a sword tool

local part = script.Parent
local givenSwords = {}

part.Touched:Connect(function(hit)
local char = hit.Parent
local humanoid = char and char:FindFirstChild(“Humanoid”)
if humanoid then
local plr = game.Players:GetPlayerFromCharacter(char)
if plr and not givenSwords[plr.UserId] then
givenSwords[plr.UserId] = true
local sword = game.ServerStorage:FindFirstChild(“ClassicSword”)
if sword then
sword:Clone().Parent = plr.Backpack
end
plr.CharacterRemoving:Connect(function()
givenSwords[plr.UserId] = nil
end)
end
end
end)

part.TouchEnded:Connect(function(hit)
local char = hit.Parent
local humanoid = char and char:FindFirstChild(“Humanoid”)
if humanoid then
local plr = game.Players:GetPlayerFromCharacter(char)
if plr then
local swordtool = plr.Backpack:FindFirstChild(“ClassicSword”) or plr.Character:FindFirstChild(“ClassicSword”)
if swordtool then
swordtool:Destroy()
givenSwords[plr.UserId] = nil
end
end
end
end)

this is the script inside the part ^ the problem is that on touchended, the sword wont destroy if plr got the sword equipped, instead it will remain in the inventory until plr will touchend the part again with sword unequipped, then, it will destroy

i would be glad if someone can help me

1 Like

It could likely be that the part.TouchEnded fires before you give the sword to the player. E.g. the script didnt find it.
Try doing WaitForChild instead of FindFirstChild or make sure that the player got the sword in his Chararcter or Backpack.

1 Like

Also when your writing scripts you can do this.

local Part = script.Parent
if Part.Anchored == false then
Part.Transparency = 0.5

IMG_0640

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.