When player leaves the part, tool isn't given

  1. Hello, i am making a shooting game and its getting really far.

  2. The problem is that am trying to do a script where players touch a part, the tool is disabled, and when the player leaves the part, the tools are avaiable for the player.

  3. If someone can help me i would be really gratefull.
    This is the script:

local Part = game.Workspace.TouchedSaveZone -- when is touched

local function on_part_touched(part, otherPart)
     --looking for player
     if otherPart.Parent:IsA("Model") and otherPart.Parent:FindFirstChild("Humanoid") then
          --search for player
          local player = otherPart.Parent.Parent
          -- Disable tool
          player.Backpack:FindFirstChild(Guns):Destroy()
     end
end

Part.Touched:Connect(on_part_touched)   -- touched ended

local function on_part_touch_ended(part, otherPart)
     -- Check if the other part is a player character
     if otherPart.Parent:IsA("Model") and otherPart.Parent:FindFirstChild("Humanoid") then
          -- Get the player object
          local player = otherPart.Parent.Parent

          -- Give the player a tool
          local tool = Instance.new("Tool")
          tool.Name = Guns
          tool.Parent = player.Backpack
     end
end

Part.TouchEnded:Connect(on_part_touch_ended())

perhaps I just don’t see it but “guns” doesn’t seem to be a variable, perhaps your script is just not working because of that

also, I don’t think .Touched has two arguments, I think it just has one
part.Touched:Connect(function(part_touched) print(part,‘touched’,part_touched) end)

also on your last line you did :Connect(on_part_touch_ended()), you’re executing the function, probably not connecting it

also you gotta use GetPlayerFromCharacter

also, I’d probably not use .Touched for things that need to be secure like tool giving/removing, I’d probably just use simple optimized worldroot queries or interactables.

P.S: your code is pretty bad, If you’re serious about making a shooting game you should probably learn to cache, optimize & secure your stuff.

1 Like

Did you try using ZonePlus instead of just a part?

ZonePlus v3.2.0 | Construct dynamic zones and effectively determine players and parts within their boundaries - Resources / Community Resources - DevForum | Roblox

1 Like

My bad, i have recently saw the problem on my script, i felt very sorry, it was because the variable guns and the use of “GetFromPlayerCharacter” ty for your answer.

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