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

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

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

✓ Accepted answer

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?

1 comment

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!

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