You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I’m trying to call a function everytime a child of a frame is clicked.
What is the issue? Include screenshots / videos if possible!
It works for the first button, but when I get to the second button it calls the function twice, the first time it’s called it is called with the name of the button that just clicked, but the second time it is called it is called with the name of the button that I first clicked.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Tried searching up solutions, couldn’t find anything of the same issue.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local plr = game.Players.LocalPlayer
local function devProduct(buttonname)
game.ReplicatedStorage.devProduct:FireServer(buttonname)
end
plr.PlayerGui:WaitForChild("ScreenGui")
for i, button in pairs(plr.PlayerGui.ScreenGui.Frames.TrollMenu.Background:GetChildren()) do
if button:IsA("ImageButton") and button.Name ~= "Spectate" and button.Name ~= "SpecRight" and button.Name ~= "SpecLeft" then
button.MouseButton1Click:Connect(function()
devProduct(button.Name)
end)
end
end
Where is this script located? If it’s located as a descendant of a ScreenGui and .ResetOnSpawn is set to true, then this script will rerun whenever you reset.
That doesn’t work in a Script located in ServerScriptService. Just an FYI.
That may be the problem, however, it doesn’t seem like it is. Make sure that the ScreenGui has it’s ResetOnSpawn property disabled, just for testing right now.
It’s definitely an issue with the for loop then. The only thing that I can think of is that the for loop is getting reran every time the second button is pressed. Is there anything that would cause this?
local Settings = require(game.ServerStorage.Settings)
local function promptDevProduct(plr, buttonname)
local userId = game:GetService("Players"):GetUserIdFromNameAsync(plr)
local button = Settings.TrollMenu[buttonname]["DevProductID"]
game:GetService("MarketplaceService"):PromptProductPurchase(plr, button)
game:GetService("MarketplaceService").PromptProductPurchaseFinished:Connect(function(userId,button,isPurchased)
if isPurchased then
Settings.TrollMenu[buttonname.. "Player"](plr)
end
end)
end
game.ReplicatedStorage.devProduct.OnServerEvent:Connect(function(plr, buttonname) promptDevProduct(plr, buttonname) end)
Is the script in ServerScriptService that the remote event is being fired to, it actually isn’t an issue with the for loop I don’t believe, whenever I print the button that is being clicked, it prints normally.
Example
-presses Kill-
-doesnt buy-
-presses Freeze-
-buys-
Freeze and Kill are both activated, even though Kill wasn’t bought.