Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I get this to work?

Ok, so what I want to do is hit a stationary object, like a non-moving brick. I want to hit it with a tool that has a script in the Handle and has the same animation as a Linked Sword. Here's the script.

enabled = true

function onTouched(h)
	if enabled == true then
		if h.Name == "PUTIS" then -- The stationary object's name that the tool will hit
			if h.Transparency ~= 1 then
			enabled = false
			h.Parent.Humanoid.Health = h.Parent.Humanoid.Health - 25 -- The stationary object has a humanoid. Whenever touched with the tool's handle, it will decrease by 25 health.
			wait(1)
			enabled = true
			end
		end
	end
end

script.Parent.Touched:connect(onTouched)

There is no output error. For some reason, the script won't do anything when I'm (the player) is stationary and hitting the stationary object. This should work since the tool is moving, allowing the Touched script to activate, but it only works when the player himself/herself is moving. I'm looking for a fix to this problem or maybe an alternative solution that doesn't include a Touched script.

4 comments

0

For the Touched event to fire, the two Parts in question must be not intersecting and then come into contact - if they are already intersecting then it won't matter if you are moving the tool or not - that may be why moving the player helps.

0

So you're saying that the tool has have Cancollide = true?

0

No, sorry, but I don't think using the Touched event will work in your case. What you need to look into is using a loop ("while" or "RenderStepped") to manually check to see if the tool is touching a PUTIS.

0

How would that look in the script? I'm having trouble understanding what you're saying.

3 answers

A tip when creating Touched event scripts, use print to see what the object is actually hitting before you set property things like names and such, as your previous code,

EDIT: Sword modification final result. This should work. If not change hit.Name to hit.Parent.Name for PUTIS. You can modify the animation or add your custom animation to the functions in the code.

-- Remodification of the Sword.

r = game:service("RunService")

sword = script.Parent.Handle
Tool = script.Parent

function blow(hit)
	if (hit.Parent == nil) then return end
		print(hit.Name)
		local humanoid = hit.Parent:findFirstChild("Humanoid")
		local vCharacter = Tool.Parent
		local vPlayer = game.Players:playerFromCharacter(vCharacter)
		local hum = vCharacter:findFirstChild("Humanoid")
			if humanoid~=nil and humanoid ~= hum and hum ~= nil then
			-- If a player is hit do stuff here.
			else
				if hit.Name == "PUTIS" then
					hum.Health = hum.Health - 25
				end
end 		end

function attack()
	local anim = Instance.new("StringValue")
	anim.Name = "toolanim"
	anim.Value = "Slash"
	anim.Parent = Tool
end

function lunge()
	local anim = Instance.new("StringValue")
	anim.Name = "toolanim"
	anim.Value = "Lunge"
	anim.Parent = Tool
	force = Instance.new("BodyVelocity")
	force.velocity = Vector3.new(0,10,0)
	force.Parent = Tool.Parent.Torso
	wait(.25)
	swordOut()
	wait(.25)
	force.Parent = nil
	wait(.5)
	swordUp()
end

function swordUp()
	Tool.GripForward = Vector3.new(-1,0,0)
	Tool.GripRight = Vector3.new(0,1,0)
	Tool.GripUp = Vector3.new(0,0,1)
end

function swordOut()
	Tool.GripForward = Vector3.new(0,0,1)
	Tool.GripRight = Vector3.new(0,-1,0)
	Tool.GripUp = Vector3.new(-1,0,0)
end

function swordAcross()
	-- parry
end

Tool.Enabled = true
local last_attack = 0
function onActivated()

	if not Tool.Enabled then return end
	Tool.Enabled = false

	local character = Tool.Parent;
	local humanoid = character.Humanoid
	if humanoid == nil then print("Humanoid not found") return end

	t = r.Stepped:wait()

	if (t - last_attack < .2) then lunge() else attack() end
	last_attack = t
	Tool.Enabled = true
end

script.Parent.Activated:connect(onActivated)
connection = sword.Touched:connect(blow)

7 comments

0

This will help to debug what is going wrong, but it's not an answer for what to do if it is actually going wrong. It should help you though, @whyOmustOitObeOme.

0

@EDIT: I also don't think Touched events will work well in this case - but it is not because it will collide with the player, unless it is animated in a weird way it will not collide with the player ;)

0

I tried the print(h.Name) script. Nothing showed up unless I moved my character. What did show up when I moved was what you'd expect, "PUTIS"

0

It actually should work, I'll repost shortly.

0

I put this in a script, but it gave me a ton of errors due to it belonging to a script where it hits an actual player. Could you simplify this for me?

0

Alright, my edits are done.

0

I'm confused as to how this works but my original script doesn't... could someone please explain this to me?

use onHit instead.

2 comments

0

How would I use an onHit in this script? (I don't know anything about onHit)

0

I'm sorry, skadadlea, but you do not understand fundamental scripting principles. OnHit is not an event, it is the name commonly given to the function conned to Touched, you can call a function whatever you like and it won't make any difference.

I don't know if bumping works in this forum, but I haven't gotten activity in this thread. This isn't an answer, only a post to get more attention on this issue.

This discussion is preserved as it was. New answers, comments, and votes are closed.