Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a way of using wait for child for script?

I am trying to make a script which would get a value from module script but i come to this problem were i am trying to get a value with waitforchild that dosent work.I know i terrible explainer. Here is the script

local player = game.Players.LocalPlayer
local MODE = script.Parent.Parent.Parent.MODE
local ItemName = script.Parent.Parent.Itemname
local Space = script.Parent.Parent.Space
local SellPrices = require(game.ReplicatedStorage.Sellprices) 
local player = game.Players.LocalPlayer
local cash = player.leaderstats.Cash

MODE.Changed:Connect(function()
	if MODE.Value == false then
		script.Parent.Parent.BUTTON.Text = "Sell"
	elseif
		MODE.Value == true then
		script.Parent.Parent.BUTTON.Text = "Put"
	end
end)

script.Parent.MouseButton1Click:Connect(function()
	print("s")
	if MODE.Value == false then
		Space.Value = false
		ItemName.Value = ""
		cash.Value = cash.Value + SellPrices:WaitForChild(ItemName.Value)-- here is the --problem
	end
end)

I hope you can help me thank you for reading.

1 answer

✓ Accepted answer

I assume you want to store and retrieve values from a modulescript,

A modulescript is in essence just a script that executes once per lua environment and is accessible by all scripts in that environment. (with lua environment i meant client (localscripts) ans server (scripts) )

If you require (for the first time in that lua environment) a modulescript it will execute it and return its result, each subsquent require will return the same result, as require caches it.

If we want to store and retrieve something from a modulescript we make it return a table. Because a table is pass by reference, with that I mean, it returns a reference to the object. If we didnt and used an string, which is pass by value. Example string: modulescript

return "Hello"

require from script 1

local module = require(modulescript) -- actually executes the module now

print(module) -- prints hello
module = "not hello"
print(module) -- prints not hello

require from script 2

wait(5) -- just so that above executes first
local module = require(modulescript)
print(module) -- prints hello, again even though we changed it

Actually in script 1 we only changed variable module to hold 'not hello'

Example table modulescript

return {message = "Hello"}

require from script 1

local module = require(modulescript) -- actually executes the module now

print(module.message) -- prints hello
module.message = "not hello"
print(module.message) -- prints not hello

require from script 2

wait(5) -- just so that above executes first
local module = require(modulescript)
print(module) -- prints not hello !

so even though the modulescript only fires once, we make changes to table it returns and those changes we can see from other scripts. Because the modulescript each time returns the reference to the table.

In your example it would be like this modulescript

local module = {}
module.Value = 5
return module

your localscript

local player = game.Players.LocalPlayer
local MODE = script.Parent.Parent.Parent.MODE
local ItemName = script.Parent.Parent.Itemname
local Space = script.Parent.Parent.Space
local sellPrices = require(game.ReplicatedStorage.Sellprices) 
local player = game.Players.LocalPlayer
local cash = player.leaderstats.Cash

MODE.Changed:Connect(function()
    if MODE.Value == false then
        script.Parent.Parent.BUTTON.Text = "Sell"
    elseif
        MODE.Value == true then
        script.Parent.Parent.BUTTON.Text = "Put"
    end
end)

script.Parent.MouseButton1Click:Connect(function()
    print("s")
    if MODE.Value == false then
        Space.Value = false
        ItemName.Value = ""
        cash.Value += sellPrices.Value -- sellprices returns 5
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.