Tool script - Simple

local DebounceTable = {}

script.Parent:WaitForChild(“Hitbox”).Touched:Connect(function(objectThatTouchesTheHitbox)
if objectThatTouchesTheHitbox.Parent then
if objectThatTouchesTheHitbox.Parent:FindFirstChild(“Humanoid”) then
if DebounceTable[objectThatTouchesTheHitbox.Parent] == true then return end
DebounceTable[objectThatTouchesTheHitbox.Parent] = true
objectThatTouchesTheHitbox.Parent.Humanoid:TakeDamage(5)
wait(5) – 5 seconds before attack
local seat = script.Parent:FindFirstChild(“Seat”)
if seat and not seat.Occupant then
seat:Sit(objectThatTouchesTheHitbox.Parent)
end
wait(5) – 5 seconds before attack
DebounceTable[objectThatTouchesTheHitbox.Parent] = nil
end
end
end)
Idk why this script doesn’t work. The players tool touches a person and then that person loses 5 hp
and the player gets seated.

Please put this in a code block using " ``` "

-- It makes your code readable. To reiterate:
Message here.
-```
 Code here
-```

Please do so, I can’t help since I’m not on RS atm and the formatting is VERY difficult to read.

1 Like

Rereading your message, this might be a possible solution. (accidently posted, await edit)

local TableOfHit = {}

local Hitbox = script.Parent:WaitForChild("Hitbox")
Hitbox.Touched:Connect(function(Object)
	if table.find(TableOfHit, Object.Parent) then return end -- If the character is in table, then return.
	
	-- Find humanoid, if not found then return.
	local Humanoid = Object.Parent:FindFirstChildWhichIsA("Humanoid")
	if not Humanoid then return end
	
	-- Insert the character and then save its position. We take 5 damage here.
	table.insert(Object.Parent)
	local Pos = #TableOfHit
	Humanoid:TakeDamage(5)
	
	-- After we take damage, we use the "task" library to multithread, as to not yield the script. Delay just waits for a certain amount of time to call a function.
	task.delay(5, function()
		local Seat: Seat = script.Parent:FindFirstChild("Seat")
		
		-- If Seat does not exist or occupants then return end.
		if not Seat then return end
		if Seat.Occupant then return end
		
		-- Sit the humanoid to the seat.
		Seat:Sit(Humanoid)
		task.wait(5)
		
		-- After 5 seconds, we remove the character from the table.
		table.remove(TableOfHit, Pos)
	end)
end)

Edit 2: Posted code

1 Like

This might be the problem, with the script that it’s the comments. Like, in Lua, comments usually start with two hyphens (–) instead of one. So, the lines that say wait(5) – 5 seconds before attack should be swapped to wait(5) -- 5 seconds before attack.

I have sent my code with explanations commentated. I hope it works and helps, let me know of any errors.

it works but the player isn’t seated when attacked

Hmm… Then that’s probably something wrong with how we find the “Seat” instance.

I suspect that it doesn’t exist, or at least the way we try to find it.

if the seat doesn’t exist, we just return. Try messing with the code within the delay function, and make sure the seat can be found.

So I just make a part called seat. Like if the player gets touched by the tool then the player sits and falls on the floor. I’m confused

You’re seat is unable to be found with:

local Seat: Seat = script.Parent:FindFirstChild("Seat")

I’m not sure how to fix it, as you handle your workspace and I can only help from pure code.
Make sure that the seat exists during runtime? Is it anchored? Does it ever get destroyed? Ask yourself questions.

Yeah it is anchored. The seat is ancoree. I used a slide script

‘’‘script.Parent.Touched:connect(function(obj)
if obj.Parent:FindFirstChild(“Humanoid”) then
obj.Parent.Humanoid.Sit = true
end
end)’‘’

local TableOfHit = {}

local Hitbox = script.Parent:WaitForChild("Hitbox")
Hitbox.Touched:Connect(function(Object)
	if table.find(TableOfHit, Object.Parent) then return end -- If the character is in table, then return.

	-- Find humanoid, if not found then return.
	local Humanoid = Object.Parent:FindFirstChildWhichIsA("Humanoid")
	if not Humanoid then return end

	-- Insert the character and then save its position. We take 5 damage here.
	table.insert(Object.Parent)
	local Pos = #TableOfHit
	Humanoid:TakeDamage(5)

	-- After we take damage, we use the "task" library to multithread, as to not yield the script. Delay just waits for a certain amount of time to call a function.
	task.delay(5, function()
		local Seat: Seat = script.Parent:FindFirstChild("Seat")

		-- If Seat does not exist or occupants then return end.
		if not Seat then return end
		if Seat.Occupant then return end

		-- Sit the humanoid to the seat.
		Seat:Sit(Humanoid)
		task.wait(5)

		-- After 5 seconds, we remove the character from the table.
		table.remove(TableOfHit, Pos)
	end)
end)
script.Parent.Touched:connect(function(obj)
	if obj.Parent:FindFirstChild("Humanoid") then
		obj.Parent.Humanoid.Sit = true
	end
end)

This is the “fixed” one but the seat timer doesn’t work.

Sorry for the late reply, I’ve been getting less active on the devforums.
If your intention is to only have the player just sit, and it doesn’t have to sit in a specific spot, then you can use this code:

local TableOfHit = {}

local Hitbox = script.Parent:WaitForChild("Hitbox")
Hitbox.Touched:Connect(function(Object)
	if table.find(TableOfHit, Object.Parent) then return end -- If the character is in table, then return.

	-- Find humanoid, if not found then return.
	local Humanoid = Object.Parent:FindFirstChildWhichIsA("Humanoid")
	if not Humanoid then return end

	-- Insert the character and then save its position. We take 5 damage here.
	table.insert(Object.Parent)
	local Pos = #TableOfHit
	Humanoid:TakeDamage(5)

	-- After we take damage, we use the "task" library to multithread, as to not yield the script. Delay just waits for a certain amount of time to call a function.
	task.delay(5, function()
        -- Just sit the humanoid we found.
        Humanoid.Sit = true
		task.wait(5)

		-- After 5 seconds, we remove the character from the table.
		table.remove(TableOfHit, Pos)
	end)
end)