So I have this function in a tool that determines what happens when the tool's handle hits a resource, and when the resourcehealth reaches 0, it drops items around the player.

The function also calls a module script in the process so I'm not sure if its a problem with the module or with the function I wrote.

The script in the tool: (I marked the function that I need help with with a comment)

--<Variables>--

local Tool = script.Parent
local character = script.Parent.Parent.Parent.Character
local meleeSettings = require(game.ServerScriptService.Modules.ToolModules.ToolSettings)
local damage = meleeSettings[Tool.Name.."Damage"]
local animationCoolDown = meleeSettings[Tool.Name.."AnimationCoolDown"]
local damageCoolDown = meleeSettings[Tool.Name.."DamageCoolDown"]
local swinging = false
local canSwing = true
local animations = require(game.ServerScriptService.Modules.ToolModules.ToolAnimations)
local sounds = require(game.ServerScriptService.Modules.ToolModules.ToolSounds)
local canDamage = true
local canFunction = true

--<Functions>--

function animationHandler()--Loads the animations
	local anim = Tool.Animations
	anim.Animation1.AnimationId = animations[Tool.Name.."Swing1"]
end

function soundHandler()--Loads the sounds
	local sound = Tool.Handle.Sounds
	sound.FleshHit.SoundId = sounds[Tool.Name.."FleshHit"]
	sound.Hit.SoundId = sounds[Tool.Name.."Hit"]
	sound.Swing.SoundId = sounds[Tool.Name.."Swing"]
end

function effects()--Puts the animation and sound together
	animationHandler()
	soundHandler()
end

function meleeActions()
	if not canSwing then return end
	canSwing = false
	effects()
	local character = Tool.Parent
	local humanoid = character.Humanoid
	local animation = humanoid:LoadAnimation(Tool.Animations.Animation1)
	animation:Play(0.4,1,1)
	wait(0.3)
	animation:AdjustSpeed(0)
	wait(0.2)
	animation:AdjustSpeed(2)
	Tool.Handle.Sounds.Swing:Play()
	swinging = true
	wait(animationCoolDown)
	swinging = false
	wait(animationCoolDown)
	canSwing = true
end

----------------------------------------------------------------

Tool.Activated:connect(function()
	meleeActions()
end)

Tool.Handle.Touched:connect(function(hit)
	if not swinging then return end
	if canDamage == false or canFunction == false then return end
	
	--<Hit Functions>--
	local function damager()
		
		canDamage = false
		if hit and hit.Parent:FindFirstChild("Humanoid") then
			
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	       	local user = game.Players:GetPlayerFromCharacter(Tool.Parent)
	
			if not player then
				
				hit.Parent.Humanoid:TakeDamage(damage)
				
			end
			if player then
				if player.TeamColor == user.TeamColor then
					
					hit.Parent.Humanoid:TakeDamage(0)

				else
					
					hit.Parent.Humanoid:TakeDamage(damage)
					
				end
			end
		end
		Tool.Handle.Sounds.FleshHit:Play()
		wait(damageCoolDown)
		canDamage = true
	end
	
	local function functioner() --Need help with this function!
		canFunction = false
		Tool.Handle.Sounds.Hit:Play()
		local ToolType = Tool.ToolType
		local ToolTypeNeeded = hit.Parent.ResourceManagement:FindFirstChild("ToolTypeNeeded") --Find the needed tool to work on this resource
		if ToolTypeNeeded.Value == ToolType.Value then --if the player is holding the correct tool then...
			local resourceHealth = hit.Parent.ResourceManagement:FindFirstChild("ResourceHealth")
			if resourceHealth.Value >= 1 then
				resourceHealth.Value = resourceHealth.Value - damage
				local function dropResources()
					if resourceHealth.Value <= 0 then
						local model = hit.Parent.Parent
						local resourceType = hit.Parent.ResourceManagement.ResourceType.Value
						local resourceModule = require(Tool.ResourceDropHandler)
						local main = hit.Parent.Parent.Body.Main --all physical parts of the model are stored in body
						local body = main.Parent
						local unanchor = body:GetChildren()
						unanchor.Anchored = false
						main:Destroy()
						model:Destroy()
						resourceModule[resourceType]()
					end
				end
				dropResources()
			end
		end
		wait(damageCoolDown)
		canFunction = true
	end
	
----------------------------------------------------------------	
	
	if hit and hit.Parent:FindFirstChild("Humanoid") then
		
		damager()
		
	elseif hit and hit.Parent:FindFirstChild("ResourceManagement") then
		
		functioner()
		
	end
	
end)

The module:

local drops = {}

function drops.SmallTree()
	local wood = game.ServerStorage.ItemDrops.Wood
	local sapling1 = game.ServerStorage.ItemDrops.Sapling1
	
	local TreeDrops = {wood, sapling1}
	
	for i = 1, 2 do
		
		local ItemDrop = math.random(1, # TreeDrops) 
		
		local Item = TreeDrops[ItemDrop]
		
		local dropped = Item:Clone()
		
		dropped.Parent = game.Workspace		
		
		dropped.Position = script.Parent.Handle.Position + Vector3.new(math.random(-3, 3), 0, math.random(-3, 3))		
		
	end
end


return drops

1 comment

0

NVM I solved it, disregard this question