Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make items drop randomly based on a model's IntValue value?

Asked by
Duksten 20
7 years ago
Edited 7 years ago

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.

001--<Variables>--
002 
003local Tool = script.Parent
004local character = script.Parent.Parent.Parent.Character
005local meleeSettings = require(game.ServerScriptService.Modules.ToolModules.ToolSettings)
006local damage = meleeSettings[Tool.Name.."Damage"]
007local animationCoolDown = meleeSettings[Tool.Name.."AnimationCoolDown"]
008local damageCoolDown = meleeSettings[Tool.Name.."DamageCoolDown"]
009local swinging = false
010local canSwing = true
011local animations = require(game.ServerScriptService.Modules.ToolModules.ToolAnimations)
012local sounds = require(game.ServerScriptService.Modules.ToolModules.ToolSounds)
013local canDamage = true
014local canFunction = true
015 
View all 130 lines...

and here is the module script:

01local drops = {}
02 
03function drops.SmallTree()
04    local wood = game.ServerStorage.ResourceDrops.Wood
05    local sapling1 = game.ServerStorage.ResourceDrops.Sapling1
06    local woodDrop = wood:Clone()
07    local saplingDrop = sapling1:Clone()
08end
09 
10 
11return 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.

1 answer

Log in to vote
0
Answered by 7 years ago

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:

01-->> First, put everything into an array
02local Drops = {Sapling1,Sapling2,Wood1,Wood2} -->> The contents of this array are examples
03 
04-->> Loop through X times
05for Iteration = 1,NumberOfDrops do
06    -->> Select a value in the array at random
07    local RandomDropIndex = math.random(1,#Drops) -->> Gets the location of a random drop
08    local RandomDrop = Drops[RandomDropIndex] -->> Indexes a value with the location from above
09    -->> Do stuff with what was selected
10    local Dropped = RandomDrop:Clone()
11    Dropped.Parent = game.Workspace
12end

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.

1Module[ResourceType]()

Hope I helped!

~TDP

0
Thanks, btw is there a way to locate the drops to the position of the resource? Duksten 20 — 7y
Ad

Answer this question