#Newcomer here! Trying to figure out how to access scripts in another object from a script

68 messages · Page 1 of 1 (latest)

rapid cave
#

Hello all! So as the title suggests, I am extremely new to Roblox Studio, and am coming here after having plenty of experience in Unity. Now what I'm trying to do is access the script of one object from another. For example:

Say the game has a script that stores amount of Wood a player has, let's call this PlayerWood. Trees spawn randomly around the map, each with a script detailing how much food the tree is worth and performs the chopping action, let's call this TreeWorth. The player walks over to a tree and chops it down. From the TreeWorth script, how can I access the PlayerWood script in order to increase the player's wood?

In Unity, this would be done by retrieving the script from the object itself. How would this kind of thing work in Roblox Studio? Any help is appreciated!

marble cape
#

Just change it in the treeworth script

rapid cave
marble cape
#

Oh I see

jagged shadow
#

however, you dont need to use a script inside, but in your example, imagine you have your playerwood script

-- Player Wood
[Define player]

local function TreeChoped(tree)
  local moduleScript = assert(tree:FindFirstChildOfClass("ModuleScript"), "No module")
  local success, module = pcall(function() require(moduleScript) end)
  assert(success, `Cant require {moduleScript.Name}`)
  player.Wood.Value += module.WoodNumber
end

[OnTreeChoped]:Connect(TreeChoped)

and then the module script that looks like

-- WoodScript (in your tree)

local TreeValues = {
  WoodNumber = 10
}

return TreeValues
#

but what you can do is somply create a NumberValue in your tree

#

Why ?

because the value can me editeds, so for example the tree a will give 10 woods, but the tree b will only give you 8

tropic zephyr
jagged shadow
jagged shadow
jagged shadow
#

happy ?

#

no but outside the joke he is right

#

my bad i forgot to require the module script

#

tell me if you dont really understand @rapid cave

rapid cave
jagged shadow
#

basically, you cant really edit the module script

#

once its here, its here and its the same for all

#

howveer, if its number value as i mentionned, you can easily edit the values

#

so for example you have 2 trees

local treeA = treeModel:Clone()
local treeB = treeModel:Clone()
#

what you can do is cloning your module script

#
local treeA = treeModel:Clone()
moduleScript:Clone().Parent = treeA
local treeB = treeModel:Clone()
moduleScript:Clone().Parent = treeB
#

but its kindof pointless since its the same for all, so you can just require the original and not the one in each tree

#

however with a NumberValue, its different

#

you can set the value that you want in it

rapid cave
#

What's NumberValue?

jagged shadow
#

basically, its an instance that holds a number

#

but you can do better by using attributes

#

basically, it doesnt create an instance, its just attribute a new custom property to the object

rapid cave
#

Oh I didn't even know that was a thing! In that case, say if each tree had an Attribute ("woodnum"), would I access that using something along the lines of

local treeA = treeModel:Clone()
local treeWood = treeA.Properties.woodnum
jagged shadow
#

haha nice try

#

but its pretty much like that yea

marble cape
#

^^ good luck w that

jagged shadow
#

so its wayyyy simplier

#

basically you can set attribute to a value with SetAttribute

#

and then querry it with GetAttribute

#

its not complucated at all

jagged shadow
marble cape
#

Aight

rapid cave
#

Ty for your help!!!

strong gobletBOT
#

studio** You are now Level 1! **studio

rapid cave
#

If it's okay I do have one more question lol

jagged shadow
#

go on

marble cape
jagged shadow
#

im gonna sleep but ill awner on phone

rapid cave
#

Say you can chop the tree just by clicking on it, simple. Is it possible to trigger TreeChopped(tree) from the script that detects the mouse over the tree + input, or should that be in the same script as TreeChopped()?

#

Sorry if that's confusing I'm just tryna understand script/object-to-script/object access here

jagged shadow
#

You can use biandable events for that

#

But it's your choice

#

You can simply have everything in a script, or having different script

rapid cave
#

Oh god I have no clue what that is so I have some googling to do

jagged shadow
#

That's my opinion, you can also here what @marble cape have to say

marble cape
jagged shadow
#

You can search for click detectors if you just want to click on the tree to chop it

#

Or tools if you wanna use tools like axe in terraria or minecraft

#

Or press e to chop

marble cape
#

Yes, click detectors should work just fine

#

Or prox-

jagged shadow
#

You can see proximity prompts,

marble cape
#

Yes, what he said

jagged shadow