How can i make this work?

Heya!

I’m struggling to figure out how i can change the image button’s image. Normally this would be easy but i am using remote events which makes it 100x more difficult

Server Code:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteEvent = ReplicatedStorage.ClaimReward

-- Detect when the player joins the game
game.Players.PlayerAdded:Connect(function(player)

	-- Define your rewards, locally this time
	local Rewards = {
		{
			Name = "1st",
			Time = 1, --120,
			Coins = 100,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "2nd",
			Time = 2, --180, -- 1200s = 20 minute,
			Coins = 200,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "3rd",
			Time = 3,--300, -- 1200s = 20 minute,
			Coins = 200,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "4th",
			Time = 4, --600, -- 1200s = 20 minute,
			Coins = 500,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "5th",
			Time = 5, --720, -- 1200s = 20 minute,
			Coins = 800,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "6th",
			Time =6, --780, -- 1200s = 20 minute,
			Coins = 900,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "7th",
			Time =7, --900, -- 1200s = 20 minute,
			Coins = 1000,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "8th",
			Time =8, --1200, -- 1200s = 20 minute,
			Coins = 200,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "9th",
			Time =9, --1500, -- 1200s = 20 minute,
			Coins = 0,
			Gems = 50,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "10th",
			Time =10, --1800, -- 1200s = 20 minute,
			Coins = 2500,
			Gems = 125,
			Pet = "Test",
			Reached = false,
			Claimed = false,
		},
		{
			Name = "11th",
			Time = 11, --2100, -- 1200s = 20 minute,
			Coins = 5000,
			Gems = 500,
			Reached = false,
			Claimed = false,
		},
		{
			Name = "12th",
			Time =12, --300, -- 1200s = 20 minute,
			Coins = 10000,
			Gems = 750,
			Pet = "Test 2",
			Reached = false,
			Claimed = false,
		},
	}

	-- Detect when the remote is fired from a client
	RemoteEvent.OnServerEvent:Connect(function(remotePlayer, rewardName)
		if remotePlayer ~= player then return end
		for i, v in ipairs(Rewards) do
			if v.Name == rewardName and v.Reached then -- If the reward is found and it is reached
				if not v.Claimed then -- Check if it was claimed already
					player.leaderstats.Coins.Value += v.Coins
					v.Claimed = true
					if v.Gems then
						player.leaderstats.Gems.Value += v.Gems
						v.Claimed = true
					end
					if v.Pet then
						print(v.Pet)
						v.Claimed = true
					end
				end
			end
		end
	end)

	-- Initialize their playtime. Note; I'm using seconds in this example, but feel free to use minutes.
	local playerPlaytime = 0

	-- Every one second
	while true and wait(1) do

		-- Add a second to their playtime
		playerPlaytime += 1

		-- Unlock rewards if playerPlaytime is enough
		for i, v in ipairs(Rewards) do
			if v.Time == playerPlaytime then
				v.Reached = true
				break
			end
		end
	end

end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Module = require(game:GetService("ReplicatedStorage"):WaitForChild("Uimodule"))

local RemoteEvent = ReplicatedStorage.ClaimReward

--// Animate \\ --

Module.AnimateButton(script.Parent:WaitForChild("Rewards").a.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").b.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").c.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").d.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").e.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").f.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").g.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").h.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").i.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").j.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").k.Vector)
Module.AnimateButton(script.Parent:WaitForChild("Rewards").l.Vector)

--// Claim Rewards \\--

local ButtonsHolder = script.Parent:WaitForChild("Rewards")

ButtonsHolder.a.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("1st")
end)

ButtonsHolder.b.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("2nd")
end)

ButtonsHolder.c.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("3rd")
end)

ButtonsHolder.d.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("4th")
end)

ButtonsHolder.e.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("5th")
end)

ButtonsHolder.f.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("6th")
end)

ButtonsHolder.g.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("7th")
end)

ButtonsHolder.h.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("8th")
end)

ButtonsHolder.i.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("9th")
end)

ButtonsHolder.j.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("10th")
end)

ButtonsHolder.k.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("11th")
end)

ButtonsHolder.l.Vector.MouseButton1Click:Connect(function()
	RemoteEvent:FireServer("12th")
end)

Thanks!

2 Likes

Could we see the UI module also making a event for every button is not useful so do this instead:

for _, v in pairs(ButtonsHolder:GetChildren()) do
 if v.Name == "a" then RemoteEvent:FireServer("1st")
 elseif v.Name == "b" then RemoteEvent:FireServer("2nd")
-- keep dong this
end

Also i recommend having the button as a number instead of a letter and sending it's name to the server
1 Like

Sorry, but this isn’t the issue i’m encountering. I’m trying to achieve the Image Id’s change when i press the button. But only when the claimed value is equal to true.

Are you changing the id’s on the server?

1 Like

Im not changing it on anything right now, i don’t know if i need to change them on the server or client

Change them on the server because exploiters can manipulate the value on the client and potentially claim whatever you are doing.

1 Like

But there are multiple Buttons, so it would change all of them

Send the server the button name and find it using that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.