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

How would you make a function go from one script to another?

Asked by 2 years ago

I know the title doesn't make sense, but I'm trying to make it so that when I press a button, a function in another script activates. Also, the other answers I found didn't work because both of my scripts are local scripts. Here is my code:

Button Script

local buy = script.Parent
local Players = game:GetService("Players")

local replicatedStorage = game:GetService("ReplicatedStorage")
local buyPressed = replicatedStorage:WaitForChild("BuyPressed")

buy.MouseButton1Click:Connect(function()
    local players = Players:GetChildren()
    for i = 1, #players do
        local player = game.Players.LocalPlayer
        local items = player.Backpack:GetChildren()
        if #items ~= 0 then
            player.Backpack:ClearAllChildren()
            buyPressed:FireServer()
        else
            buyPressed:FireServer()
        end
    end
end)

Main Script

local buyPressed = game.ReplicatedStorage:WaitForChild("OnClick")

            local function buyItem()
                local result = buyTool:InvokeServer(tool, tool.Cost)

                if result == "BoughtItem" then
                    print("item bought")
                elseif result == "NotEnoughCash" then
                    print("not money")
                elseif result == "NoItemOrNoCostValue" then
                    print("this items messed up bruh")
                end
            end

            buyPressed.OnServerEvent:Connect(buyItem)

1 answer

Log in to vote
0
Answered by 2 years ago

On line 1 in the main script, it says you are waiting for the event "OnClick", but on the other script in line 4 you are waiting for a different event called "BuyPressed". Could this be your answer?

0
@ItzFrostTheWolf right after I posted this I found this in the code and I fixed it, but it is really glitchy now but works. Thanks! Borkingforlife 9 — 2y
Ad

Answer this question