Hi guys, so I’m trying to implement a function as a module function instead for my game since it’s a tycoon (it has a lot of buttons and thus scripts necessary to make them work) and I’m unsure how to reference the correct button instances (there’s multiple buttons with instance names that are identical) when the script is calling the module function.
The line in the module function that’s being called causing the error:
local price = script.Parent.Parent.Configuration.Price.Value
For context:
Module script in SSS:
local module = {}
function module.onTouched(hit)
local debounce = 0
local price = script.Parent.Parent.Configuration.Price.Value
local config = script.Parent.Parent.Configuration
local buytargetname = script.Parent.Parent.Configuration.ObjectName.Value
local checkpoint = script.Parent.Parent.Configuration.Checkpoint.Value
local cmin = config.ButtonNumMin.Value
local cmax = config.ButtonNumMax.Value
local text1 = script.Parent.Parent.TextGui.Text1Sur.Text
local text2 = script.Parent.Parent.TextGui.Text2Sur.Text
text1.Text = ("Buy "..buytargetname.." for "..price)
text2.Text = ("Buy "..buytargetname.." for "..price)
if debounce == 0
then
debounce = 1
local check = hit.Parent:FindFirstChild("Humanoid") -- check == humanoid
if check ~= nil then -- if part that touched is human then
local user = game.Players:GetPlayerFromCharacter(hit.Parent) -- user == Player
local stats = user:FindFirstChild("leaderstats")
if config.Multiplier.Value == true then -- checks if button has currency multiplication option enabled or not
if stats ~= nil then
local cash = stats:FindFirstChild("Currency")
if cash.Value > price then -- if player cash more than price then
cash.Value = cash.Value - price -- player cash - price
script.Parent.Purchased.Value = true
if script.Parent.Purchase.Playing == false then
script.Parent.Purchase:Play()
end
game:GetService("ServerScriptService").ServerConfig.CurrencyMultiplier.Value = game:GetService("ServerScriptService").ServerConfig.CurrencyMultiplier.Value*2
print("Server Multiplier set to "..game:GetService("ServerScriptService").ServerConfig.CurrencyMultiplier.Value)
if checkpoint == true then
for i = cmin,cmax do
if game:GetService("ReplicatedStorage").TycoonButtons:FindFirstChild("TycoonButton ["..i.."]") ~= nil then
local buttonclone = game:GetService("ReplicatedStorage").TycoonButtons["TycoonButton ["..i.."]"]:clone()
buttonclone.Parent = workspace
wait(0.5)
if script.Parent.Purchase.Playing == false then
script.Parent.Purchase:Play()
end
else
warn("TycoonButton ["..i.."] does not exist! Checkpoint invalid!")
end
end
end
wait(2.75)
script.Parent.Parent:Destroy()
elseif cash.Value < price then
if script.Parent.Denied.Playing == false and script.Parent.Purchased.Value == false then
script.Parent.Denied:Play()
end
end
end
else
if stats ~= nil then
local cash = stats:findFirstChild("Currency")
if cash.Value > price then
cash.Value = cash.Value - price
script.Parent.Purchased.Value = true
if game:GetService("ReplicatedStorage").TycoonObjects:FindFirstChild(buytargetname) ~= nil then
local clonedobj = game:GetService("ReplicatedStorage").TycoonObjects[buytargetname]:Clone()
clonedobj.Parent = workspace
if script.Parent.Purchase.Playing == false then
script.Parent.Purchase:Play()
end
else
warn(buytargetname.." does not exist in the tycoonobjects folder!")
end
if checkpoint == true then
for i = cmin,cmax do
if game:GetService("ReplicatedStorage").TycoonButtons:FindFirstChild("TycoonButton ["..i.."]") ~= nil then
local buttonclone = game:GetService("ReplicatedStorage").TycoonButtons["TycoonButton ["..i.."]"]:clone()
buttonclone.Parent = workspace
wait(0.5)
if script.Parent.Purchase.Playing == false then
script.Parent.Purchase:Play()
end
else
warn("TycoonButton ["..i.."] does not exist! Checkpoint invalid!")
end
end
end
wait(2.75)
script.Parent.Parent:Destroy()
elseif cash.Value < price then
if script.Parent.Denied.Playing == false and script.Parent.Purchased.Value == false then
script.Parent.Denied:Play()
end
end
end
end
end
end
debounce = 0
end
return module
Button script that calls the function (model parented by workspace):
text1.Text = ("Buy "..buytargetname.." for "..price)
text2.Text = ("Buy "..buytargetname.." for "..price)
local modulefort = require(game.ServerScriptService.ButtonModule)
script.Parent.Touched:Connect(function(hit)
modulefort.onTouched(hit)
end)
Again to make sure people understand, I have no idea how to go about referencing the instances within the button that contains that script that’s calling the module function and the closest I got to in terms of ideas was:
local price = game.Workspace["Tycoon Button [1]"].Configuration.Price.Value
But of course, that’s only going to reference the first button and its instances even if the script that’s calling the module function isn’t from said button and that’s obviously not what I made that module function for. If anyone could give help on how to solve this that would be great.