Safe Zone for a sword fighting game

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:
image

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.

2 Likes

This does work, thank you! I didn’t know you could set the visibilty of ForceFields, so knowing this makes everything much easier.

I still need to figure out how to move Swords from ServerStorage to the player’s backpack, but I think I could find a tutorial on that somewhere.

1 Like

local clone = path.to.weapon.sword:Clone()

clone.Parent = path.to.player.backpack

That should be the gist of what you’ll need to do

Edit: for auto equipping, you’ll want to parent it to the player instead.

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.

The sword is under ServerStorage
image

I believe it’s because you aren’t using any sort of debounce.

Please look up debounce Roblox Lua, on Google, implement it, and if you still have issues, report back

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.

That’s locally though, make sure you do the back pack logic on the server side.

Once you obtain the backpack, I believe, every child is a tool.

So loop through all children of the backpack, if the .Name is of SWORD (or w/e it’s named), then either :Destroy() it or add it to the game.debris.

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.

This is one solution.

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