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

01local buy = script.Parent
02local Players = game:GetService("Players")
03 
04local replicatedStorage = game:GetService("ReplicatedStorage")
05local buyPressed = replicatedStorage:WaitForChild("BuyPressed")
06 
07buy.MouseButton1Click:Connect(function()
08    local players = Players:GetChildren()
09    for i = 1, #players do
10        local player = game.Players.LocalPlayer
11        local items = player.Backpack:GetChildren()
12        if #items ~= 0 then
13            player.Backpack:ClearAllChildren()
14            buyPressed:FireServer()
15        else
16            buyPressed:FireServer()
17        end
18    end
19end)

Main Script

01local buyPressed = game.ReplicatedStorage:WaitForChild("OnClick")
02 
03            local function buyItem()
04                local result = buyTool:InvokeServer(tool, tool.Cost)
05 
06                if result == "BoughtItem" then
07                    print("item bought")
08                elseif result == "NotEnoughCash" then
09                    print("not money")
10                elseif result == "NoItemOrNoCostValue" then
11                    print("this items messed up bruh")
12                end
13            end
14 
15            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