How would I make items drop randomly based on a model's IntValue value?
Asked by
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.
003 | local Tool = script.Parent |
004 | local character = script.Parent.Parent.Parent.Character |
005 | local meleeSettings = require(game.ServerScriptService.Modules.ToolModules.ToolSettings) |
006 | local damage = meleeSettings [ Tool.Name.. "Damage" ] |
007 | local animationCoolDown = meleeSettings [ Tool.Name.. "AnimationCoolDown" ] |
008 | local damageCoolDown = meleeSettings [ Tool.Name.. "DamageCoolDown" ] |
009 | local swinging = false |
011 | local animations = require(game.ServerScriptService.Modules.ToolModules.ToolAnimations) |
012 | local sounds = require(game.ServerScriptService.Modules.ToolModules.ToolSounds) |
013 | local canDamage = true |
014 | local canFunction = true |
018 | function animationHandler() |
019 | local anim = Tool.Animations |
020 | anim.Animation 1. AnimationId = animations [ Tool.Name.. "Swing1" ] |
023 | function soundHandler() |
024 | local sound = Tool.Handle.Sounds |
025 | sound.FleshHit.SoundId = sounds [ Tool.Name.. "FleshHit" ] |
026 | sound.Hit.SoundId = sounds [ Tool.Name.. "Hit" ] |
027 | sound.Swing.SoundId = sounds [ Tool.Name.. "Swing" ] |
035 | function meleeActions() |
036 | if not canSwing then return end |
039 | local character = Tool.Parent |
040 | local humanoid = character.Humanoid |
041 | local animation = humanoid:LoadAnimation(Tool.Animations.Animation 1 ) |
042 | animation:Play( 0.4 , 1 , 1 ) |
044 | animation:AdjustSpeed( 0 ) |
046 | animation:AdjustSpeed( 2 ) |
047 | Tool.Handle.Sounds.Swing:Play() |
049 | wait(animationCoolDown) |
051 | wait(animationCoolDown) |
057 | Tool.Activated:connect( function () |
061 | Tool.Handle.Touched:connect( function (hit) |
062 | if not swinging then return end |
063 | if canDamage = = false or canFunction = = false then return end |
066 | local function damager() |
069 | if hit and hit.Parent:FindFirstChild( "Humanoid" ) then |
071 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
072 | local user = game.Players:GetPlayerFromCharacter(Tool.Parent) |
076 | hit.Parent.Humanoid:TakeDamage(damage) |
080 | if player.TeamColor = = user.TeamColor then |
082 | hit.Parent.Humanoid:TakeDamage( 0 ) |
086 | hit.Parent.Humanoid:TakeDamage(damage) |
091 | Tool.Handle.Sounds.FleshHit:Play() |
096 | local function functioner() |
098 | local ToolType = Tool.ToolType |
099 | local ToolTypeNeeded = hit.Parent.ResourceManagement:FindFirstChild( "ToolTypeNeeded" ) |
100 | if ToolTypeNeeded.Value = = ToolType.Value then |
101 | local resourceHealth = hit.Parent.ResourceManagement:FindFirstChild( "ResourceHealth" ) |
102 | if resourceHealth.Value > = 1 then |
103 | resourceHealth.Value = resourceHealth.Value - damage |
104 | Tool.Handle.Sounds.Hit:Play() |
105 | local function dropResources() |
106 | if resourceHealth.Value < = 0 then |
107 | local resourceType = hit.Parent.ResourceManagement.ResourceType.Value |
108 | local resourceModule = require(game.ServerScriptService.Modules.ResourceModules.ResourceDropModule) |
109 | resourceModule.resourceType() |
120 | if hit and hit.Parent:FindFirstChild( "Humanoid" ) then |
124 | elseif hit and hit.Parent:FindFirstChild( "ResourceManagement" ) then |
and here is the module script:
03 | function drops.SmallTree() |
04 | local wood = game.ServerStorage.ResourceDrops.Wood |
05 | local sapling 1 = game.ServerStorage.ResourceDrops.Sapling 1 |
06 | local woodDrop = wood:Clone() |
07 | local saplingDrop = sapling 1 :Clone() |
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.