Please Help Me With My Script

Screenshot 2023-08-17 at 7.49.49 PM
I am finalizing my roblox story game but cant seem to get a really simple script working is there anyone who can help?

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Sword = script.Parent:WaitForChild("Sword")
local Slash = script:WaitForChild("Slash")
local Debounce = false
local Hitting = {}

Tool.Activated:Connect(function()
	
	if Debounce == false then
		Debounce = true
		
		local Humanoid = Tool.Parent:FindFirstChild("Humanoid")
		local AnimTrack = Humanoid:LoadAnimation(Slash)
		
		AnimTrack:Play()
		local sound2 = Instance.new("Sound")
		sound2.PlayOnRemove = true
		sound2.SoundId = "rbxassetid://147722227"
		sound2.Volume = 1
		sound2.TimePosition = 1
		sound2:Destroy()
		wait(1)
		Debounce = false
	end
end)

Sword.Touched:Connect(function(Hit)
	
	if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent ~= Tool.Parent then
		
		if Debounce == true and Hitting[Hit.Parent] == nil then
			
			if Hit.Parent:findFirstChild("ForceField") then
				if Hit.Parent.ForceField.Visible == true then
					return
				end
			end
			
			Hit.Parent.Humanoid.Health = Hit.Parent.Humanoid.Health - 10
			
			local sound = Instance.new("Sound",Hit.Parent)
			sound.PlayOnRemove = true
			sound.SoundId = "rbxassetid://4752240634"
			sound.Volume = 1
			sound.TimePosition = 1
			sound:Destroy()
			
			Hitting[Hit.Parent] = true
			wait(1)
			Hitting[Hit.Parent] = nil
		end
	end
end)

For context I want a hit sound and a swing sound, the sword cant damage any players in the server and only npcs in server. And when you hit a hit effect comes up sort of like this
Screenshot 2023-08-17 at 7.51.23 PM

1 Like

the reason it might not be hitting players is because the hit object is an accessory handle
which would be Hit.Parent.Parent:FindFirstChild("Humanoid") to access Humanoid in that case

you could fix it this way

-- since Character is always a model find first reference of a model
local Character = Hit:FindFirstAncestorOfClass("Model")
-- if Character is nil short-circuit and operator
local Humanoid = Character and Character:FindFirstChild("Humanoid")

-- why using findfirstchild function if Character is not nil as it must have humanoid?
-- > incase if the hit object is descendant of a model that isnt a Character

if Humanoid then
    -- proceed doing damage and applying effects
end

to make hit particles you will need to create the particle within the script or clone premade one for example from replicatedstorage then parent it to Character torso or any body part and using Emit method of ParticleEmitter instance emit effects

local HitEffect = ReplicatedStorage.HitEffect:Clone()

HitEffect.Parent = Character.HumanoidRootPart
HitEffect:Emit(1) -- number of particles to emit

task.wait(HitEffect.Lifetime) -- wait for effect to complete before destroying it

HitEffect:Destroy()

also i would suggest to use GetTouchingParts or GetPartsInPart (GetPartsInPart is the latest version of GetTouchingParts) instead of touched event to detect hits for weapons

thx i was able to make a system and now its working

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