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

I made a sword script and I have this enemy but he is not taking any damage? Why?

This is the script im using

local sword = script.Parent
local canAttack = true
local canDamage = false

function onTouch(part)

	local character = part.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	
	
	if player then
		if canDamage == true then
			character.BoarHumanoid:TakeDamage(2)
		end
	end
	
end
sword.Blade.Touched:Connect(onTouch)

function Attack()
	
	local attack = sword.Parent.Humanoid:LoadAnimation(script.SwordSlash)
	if canAttack == true then
		attack:Play()
		canAttack = false
		
		canDamage = true
		wait(1)
		
		canDamage = false
		wait(0.25)
		canAttack = true
	end
	
end
sword.Activated:Connect(Attack)

4 comments

0

Put a print inside the onTouch() function and playtest, and see if it prints. If it doesn't, that means you're gonna have to make custom hitboxex or something that makes it easier to hit,

0

Okay Let me try that

0

It prints but still nothing is going on

0

its giving me the error: Blade is not a valid member of Tool "Workspace.Tool" But I have a part called Blade in the tool so idk?

2 answers

I noticed two major mistakes in your code:

  • You're checking an NPC to see if it is a player, which will never happen.
  • The Tool is not actually parented to a player's Backpack, therefore Activated fails to work. This may also be the reason you're getting an error.

Solutions:

  1. Allow the player to use the tool. Putting the tool in StarterPack or assigning its parent to the player's Backpack both work. If you parent the tool to the player's Character, the player will automatically equip the tool.

  2. Check for not game.Players:GetPlayerFromCharacter(character). I think you're getting confused with this function, but adding not to it is telling the script to check to see if there is no player associated with character.

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