local IsTouching = false
script.Parent.Touched:connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if hum and not IsTouching then
IsTouching = true
if plr.Backpack:FindFirstChild("Sword") == nil then
game.ServerStorage.Sword:clone().Parent = plr.Backpack
end
end
end)
script.Parent.TouchEnded:connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if hum and IsTouching then
IsTouching = false
for _,v in pairs(plr.Character:GetChildren()) do
if v:IsA("Tool") and v.Name == "Sword" then
v:Destroy()
end
end
end
end)
Above is a server-side script located inside the part that the player has to be standing on in order to have a Sword in their inventory.
The goal of this script is to make it so they can only sword fight when they are within the arena limits. However the issue I’m having with it right now is it won’t let you equip it. It continues to remove your sword and assign you a new one. Making it impossible to equip a sword for longer then a second.
I’m very rusty at scripting, where am I going wrong? D:
The event you connected this function to is TouchedEnded, which will fire when the player stops touching the sword dispenser. And when the function is called, it searches for any sword in the player’s inventory and just straight up deletes them.
Let me explain further. They are standing on top of the “sword giver.” So I am not stepping off of the platform and it is still removing the sword.
Then I’m not sure. I’m basing my conclusion on the code snippet you provided us. TouchEnded is an event that fires when the player walks away from the sword giver.
You can show us more of the script so we can get a better understanding of it
local Register = {}
local approxPartRadius = (part.Size.X + part.Size.Y + part.Size.Z) / 2
local function OnTouch(hit)
local root = hit.Parent:FindFirstChild("HumanoidRootPart")
if not root then return end --non-humanoid trigger
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end --NPC trigger
if Register[player] then return end --player is already registered
Register[player] = true
--Do some stuff here
--keep checking character position, and remove from register if player leaves
--also keep checking Parent to make sure character is still in the game
while hit.Parent and (root.Position - part.Position).Magnitude < approxPartRadius do
wait(.1)
end
--this section activates on player leaving the area
Register[player] = nil
end
part.Touched:Connect(OnTouch)
I forgot where I found it but it is a post somewhere on here