I want to make a feature where once a resource (like a tree or rock) is broken by a tool, a random amount of items will drop based on an IntValue in the resource model named "ResourceType".
visual example: https://prnt.sc/fzu357
In the image shown in the link, the value of ResourceType is set to "SmallTree", and so when the script in the tool detects what value it is, it will request a function from a module that will be for that specific name, determining what items and also the amount of it that will be dropped.
So in the case of the value "SmallTree", the script it will call for a function in the module that has the exact name as the value in the function name.
The problem is, I dont know where to go from there, I know I want the amount of items that are dropped to be random, but I'm stuck on that part too.
Heres the script in the tool. I was originally only going to post on the part of the code that I need help with, but I've decided that posting only part of the code leaves some important defined functions and variables out, which might confuse you guys. The --??? marks where I am currently stuck at.
--<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() canFunction = false local ToolType = Tool.ToolType local ToolTypeNeeded = hit.Parent.ResourceManagement:FindFirstChild("ToolTypeNeeded") if ToolTypeNeeded.Value == ToolType.Value then local resourceHealth = hit.Parent.ResourceManagement:FindFirstChild("ResourceHealth") if resourceHealth.Value >= 1 then resourceHealth.Value = resourceHealth.Value - damage Tool.Handle.Sounds.Hit:Play() local function dropResources() if resourceHealth.Value <= 0 then local resourceType = hit.Parent.ResourceManagement.ResourceType.Value local resourceModule = require(game.ServerScriptService.Modules.ResourceModules.ResourceDropModule) resourceModule.resourceType() --??? end end 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)
and here is the module script:
local drops = {} function drops.SmallTree() local wood = game.ServerStorage.ResourceDrops.Wood local sapling1 = game.ServerStorage.ResourceDrops.Sapling1 local woodDrop = wood:Clone() local saplingDrop = sapling1:Clone() end return drops
I am mostly stuck on the module script though, I'm not really sure what stuff will be better off on the module and what stuff will be better off in the script.
Sorry if this stuff is a bit much for you guys, but any help or suggestions will be gladly appreciated.
Alright, your code mainly seems to be fine.
I would recommend your tool's code to be in a LocalScript
, and have it communicate with a normal script via a RemoteFunction/Event
, where it will do things from there.. but that may be a bit too complicated for the moment, and is not your question.
Here's how you can make random drops:
-->> First, put everything into an array local Drops = {Sapling1,Sapling2,Wood1,Wood2} -->> The contents of this array are examples -->> Loop through X times for Iteration = 1,NumberOfDrops do -->> Select a value in the array at random local RandomDropIndex = math.random(1,#Drops) -->> Gets the location of a random drop local RandomDrop = Drops[RandomDropIndex] -->> Indexes a value with the location from above -->> Do stuff with what was selected local Dropped = RandomDrop:Clone() Dropped.Parent = game.Workspace end
So, you could turn that into a function in your ModuleScript
.
I also noticed that you didn't index the function in the ModuleScript correctly in your tool script - if you're using a variable, you will need to use [variable]
instead of .variable
.
Module[ResourceType]()
Hope I helped!
~TDP