Why does my image button click only work in studio but not in Roblox player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make it so when the image button is clicked that the leaderstat and gui update their values after being pressed.

  2. What is the issue? Include screenshots / videos if possible!
    The issue I am experiencing is that when I try it in the Roblox player, it no longer works but when testing it in Roblox studio it works just as intended.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried changing the event from activated to inputbegan and mousebutton1click. Also tried to make sure the player was fully loaded in the game by adding some waits and using character added but still will not work in the Roblox player.

Here is a copy of my local script that I have in startergui

local players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local soundsFolder = ReplicatedStorage:WaitForChild("SoundsFolder")
local soundList = soundsFolder:GetChildren() -- Get all sounds in the folder
local player = players.LocalPlayer or players:WaitForChild("LocalPlayer")
local screenGui = player.PlayerGui:WaitForChild("ScreenGui")
local imageButton = screenGui:WaitForChild("ImageButton")
local leaderstats = player:WaitForChild("leaderstats")
local clicksValue = leaderstats:FindFirstChild("Clicks")
local debounce = false
local debounceTime = 0.1  -- Adjust this time as necessary
local updateClicksEvent = ReplicatedStorage:FindFirstChild("UpdateClicks")
local confetti = require(game.ReplicatedStorage:WaitForChild("ConfettiScript"))
local confettiTable = {
	100,
	200,
	300,
	410,
	500,
	600,
	678,
	777,
	1000
}

-- Update the TextLabel with the current Clicks value initially
screenGui:WaitForChild("TextLabel").Text = tostring(clicksValue.Value)

-- Function to play a random sound
local function playRandomSound()
	if #soundList > 0 then -- Ensure there are sounds in the folder
		local randomSound = soundList[math.random(1, #soundList)] -- Select a random sound
		randomSound:Play() -- Play the selected sound
	else
		warn("No sounds found in the folder!")
	end
end

-- Function to check if confetti should play
local function checkConfetti(newValue)
	for _, threshold in ipairs(confettiTable) do
		if newValue == threshold then
			playRandomSound()
			confetti.Play()
			break
		end
	end
end

-- Update the TextLabel whenever the Clicks value changes in leaderstats
clicksValue.Changed:Connect(function(newValue)
	screenGui.TextLabel.Text = tostring(newValue)
	checkConfetti(newValue)
end)

-- Increment Clicks when the player clicks the image
imageButton.Activated:Connect(function()
	if not debounce then
		debounce = true
		updateClicksEvent:FireServer(clicksValue.Value + 1)
		task.wait(debounceTime)
		debounce = false
	end
end)

-- Initial check to see if confetti should play (if the player already has enough clicks)
checkConfetti(clicksValue.Value)
1 Like

I also want to add, I feel as if the issue is with this particular part of my code:


– Increment Clicks when the player clicks the image
imageButton.Activated:Connect(function()
if not debounce then
debounce = true
updateClicksEvent:FireServer(clicksValue.Value + 1)
task.wait(debounceTime)
debounce = false
end
end)

Try switchin Activated to MouseButton1Click; it still works on all devices

1 Like

Thank you, I tried that but still does not work in Roblox player, only works in Roblox studio. Tested on both Mac and mobile android phone too.

Code looks good to me, it’s possible it’s never connecting the click function
try adding a print before it and inside it

I had an issue with this too. My image buttons we’re working on PC and Console,but wasn’t working on Mobile devices for no reason.

I fixed this issue by using the ImageButton.InputBegan:Connect(function(Input).

This event detects inputs like Mouse,Keyboard,Gamepad or even TouchMovements.

imageButton.InputBegan:Connect(function(Input)
    if Input.UserInputType == Enum.UserInputType.Touch or Input.UserInputType == Enum.UserInputType.MouseButton1 then
	    if not debounce then
		    debounce = true
		    updateClicksEvent:FireServer(clicksValue.Value + 1)
		    task.wait(debounceTime)
		    debounce = false
	    end
    end
end)

I updated your imagebutton click event and replaced the evnet with InputBegan.

Can you check if it is working now?

1 Like

I originally had my code like that but I tried it again and still does not work in the Roblox player. Activated, mousebutton1click, and input began all work fine in Roblox studio but doesn’t work at all in Roblox player.

Why do you have a script in starter gui? Move it to starterplayerscripts and update the code. Also you can still open the console in roblox player, see if anything comes up. Maybe you unchecked Filtering, click on serverscriptservice and make sure it is checked.

Technically the script is in the image button. Could you kindly tell me what is wrong with It being in startergui?

There is nothing wrong with having it in StarterGui, as it gets copied into player.PlayerGui either way. It’s up to personal preference, really.