I’m making a sword fighting game, and I’m trying to make a safe zone so that when the player is touching it, they don’t have a sword and they have infinite health (I don’t want a forcefield as that kind of looks ugly in my opinion). Once they leave the safe zone, I tried making it so that the player will have 3 seconds of invincibility before they get their sword and their health is back to normal.
I also couldn’t find anything about adding the sword to the player’s inventory, but I know it involves moving the Sword from ServerStorage to StarterPack if I remember correctly, I just don’t remember where I saw that.
local health = 100
script.Parent.Touched:Connect(function(part)
if(part.Name ~= "HumanoidRootPart") then
local character = part.Parent
local healthParent = character.Health
health = health + 100
end
end)
script.Parent.TouchEnded:Connect(function(part)
if(part.Name ~= "HumanoidRootPart") then
local character = part.Parent
wait(3)
health = 100
end
end)
Here’s an image of my explorer if that helps at all:
I’m going to be making custom swords in the mean time because that’s something I can do!
Try using forcefields, but to disable the look, just clone one, put it into the player’s character, and turn the forcefields visibility to false. That should do the trick.
I tried to do what you said but it gave me two swords (one not within 3 seconds and one after 3 seconds) and now my forcefield won’t destroy.
script.Parent.TouchEnded:Connect(function(part)
if(part.Name ~= "HumanoidRootPart") then return end
local character = part.Parent
local forceField = character:FindFirstChild("Safezone")
if(forceField) then
wait(3)
forceField:Destroy()
local clone = game.ServerStorage.Swords.ClassicSword:Clone()
clone.Parent = character
end
local player = game.Players:GetPlayerFromCharacter(character)
if(player) then
event:FireClient(player, "Out")
end
end)
That’s the whole of the TouchEnded script, I think I pathed the player to the correct area, and the clone too but I’m not entirely confident.
I got the debounce to work, now I’m trying to make it so when the player gets back into the zone, their backpack gets cleared of the sword.
script.Parent.Touched:Connect(function(part)
if(part.Name ~= "HumanoidRootPart") then return end
local character = part.Parent
local forceField = Instance.new("ForceField")
forceField.Name = "Safezone"
forceField.Parent = character
forceField.Visible = true
local backpack = character:FindFirstChildOfClass("Backpack")
local player = game.Players:GetPlayerFromCharacter(character)
if(player) then
event:FireClient(player, "In")
end
end)
This is all I have right now for the Touched script, I have the backpack but I don’t know how to actually clear it. I’ve tried a couple things but they’ve broken the whole script.
I’ve updated the script a lot, but it still keeps bugging out and I don’t really know why.
script.Parent.Touched:Connect(function(part)
if(part.Name ~= "HumanoidRootPart") then return end
--if not game.Players:GetPlayerFromCharacter(part.Parent) then return end
local character = part.Parent
local forceField = Instance.new("ForceField")
forceField.Name = "Safezone"
forceField.Parent = character
forceField.Visible = true
local player = game.Players:GetPlayerFromCharacter(character)
local backpack = player:FindFirstChild("Backpack")
if backpack then
for _, child in pairs(backpack:GetChildren()) do
if child:IsA("Tool") then
child.Enabled = false
end
end
end
--for _, tool in ipairs(backpack:GetChildren()) do
-- if tool:IsA("Tool") then
-- tool:Destroy()
-- end
--end
if player.Character:FindFirstChildOfClass("Tool") then
player.Character:FindFirstChildOfClass("Tool"):Destroy()
end
if(player) then
event:FireClient(player, "In")
end
end)
What happens is when the player enters back into the safezone while having the tool equipped, it starts looping the code for some reason. I think it has to do with the fact that Touched repeats every time you move basically, so unless there is a way to make it so it’s onTouch then I don’t really know how this could be fixed.
There’s a few ways. You can create a table outside of the function scope, and when a player touches the part, if they are not in the table, add them to it and run the logic. Otherwise do nothing.
When a player leaves, remove them from the table if they are in there.