How to change text label's Text with a event

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 change textlabel’s text to Tool’s Strenght Value when ToolActivated

  1. What is the issue? Include screenshots / videos if possible!

I need help with understanding OnClientEvent or OnServerEvent

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!

-- ToolManager- a normal script

local replicatedStorage= game:GetService("ReplicatedStorage")
local ServerStorage= game:GetService("ServerStorage")
local tools = ServerStorage:FindFirstChild("Tools")
local scripts = ServerStorage:FindFirstChild("Script")
local toolConfig = require(replicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))
local remotes = replicatedStorage:FindFirstChild("Remotes")
local kelenko = game:WaitForChild("StarterGui"):FindFirstChild("CurrencyGui"):FindFirstChild("kelenko")

remotes.ToolActivated.OnServerEvent:Connect(function(player, Player)
	local playerTool = player.Inventory.EquippedTool.Value
	for tool, toolTable in pairs(toolConfig) do
		if tool == playerTool then
			player.leaderstats.Strenght.Value += toolTable.Strenght
			remotes.TextChange:FireClient()
		end
	end
end)


for _,tool in pairs(tools:GetChildren()) do
	local script = scripts.ToolActivated:Clone() -- Bunu template al
	script.Parent= tool
end
-- localScript Child of Textlabel 
local replicatedStorage= game:GetService("ReplicatedStorage")
local remotes = replicatedStorage:FindFirstChild("Remotes")
local kelenko = game:WaitForChild("StarterGui"):FindFirstChild("CurrencyGui"):FindFirstChild("kelenko")
local toolConfig = require(replicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))

remotes.TextChange.OnClientEvent:Connect(function(player,Player)
	local playerTool = player.Inventory.EquippedTool.Value
	for tool, toolTable in pairs(toolConfig) do
		if tool == playerTool then	
			kelenko.Visible = true
			kelenko.Text = toolTable.Strenght
		end
    end	
end)
--  LocalScript Child of tools
local replicatedStorage= game:GetService("ReplicatedStorage")
local remotes = replicatedStorage:FindFirstChild("Remotes")
	
local tool = script.Parent

tool.Activated:Connect(function()
	remotes.ToolActivatedRemote:FireServer()
	print("text")
end)

it seems like I am doing something wrong with usage of OnClientEvent, OnServerEvent Usage.
please help.

I don’t know if you need them but here is the screenShot of the scripts I mentioned with their locations



Yeah still waiting for ideas >>30Charr

Well there are a few things that should be taken in count before trying to fix it.

The system wont work if you are trying to reach the StarterGui. Thats just the GUI cloned when any new player joins and given to player.
local kelenko = game:WaitForChild("StarterGui"):FindFirstChild("CurrencyGui"):FindFirstChild("kelenko")

The communication between remotes is easy. About your system I think theres certain gaps.

You already using a Module to handle the stats of tools. Theres no reason to have module in ReplicatedStorage to read this, and theres no reason to read that module twice from client and server. Once server readed the server module to get the stats just send back that data to client to populate client’s GUIs

Im using an edited version of your scripts for this example, I changed a few things:

A local script in tool to trigger the acticate tool remote:

--  LocalScript Child of tools
local replicatedStorage = game:GetService("ReplicatedStorage")
local remotes = replicatedStorage:FindFirstChild("Remotes")

local tool = script.Parent

-- Player activated tool on client side
-- This Activated event should be in a ServerScript inside the tool instead of client local script
tool.Activated:Connect(function()
	remotes.ToolActivated:FireServer() -- Asking server function connected to ToolActivated remote
	print("ToolActivated from Client")
end)

A server script in ServerScriptService to listen when a player activated that tool:

-- Tool Manager SV script

local replicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local tools = ServerStorage:FindFirstChild("Tools")
local scripts = ServerStorage:FindFirstChild("Script")

local remotes = replicatedStorage:FindFirstChild("Remotes")

-- Never in Client, keep your tables in server:
--local toolConfig = require(replicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))
local toolConfig = require(game.ServerScriptService:WaitForChild("Tools"):WaitForChild("ToolConfig"))

-- Client activated a tool REMevent
remotes.ToolActivated.OnServerEvent:Connect(function(player)
	warn("Client", player, "activated a tool")
	print("Find tool's stats in SV module of:", player.Inventory.EquippedTool.Value)
	
	-- Find tool's stats in module taking in count Player EquippedTool value:
	if toolConfig[player.Inventory.EquippedTool.Value] then -- if toolConfig["Red"] Blue, etc is found in SV module
		player.leaderstats.Strength.Value = toolConfig[player.Inventory.EquippedTool.Value]["Strength"]
		remotes.TextChange:FireClient(player, toolConfig[player.Inventory.EquippedTool.Value]["Strength"]) -- Sending updated data back to client	
	end
end)

-- Adding scripts to tools in SS
for _, tool in pairs(tools:GetChildren()) do
	local script = scripts.ToolActivated:Clone() -- Bunu template al
	script.Parent = tool
end

-- FOR RUN TESTING, Creating instances in Player
game.Players.PlayerAdded:Connect(function(player)
	local leadFold = Instance.new("Folder")
	leadFold.Name = "leaderstats"
	leadFold.Parent = player
	
	local Value = Instance.new("IntValue")
	Value.Name = "Strength"
	Value.Parent = leadFold
	
	local Folder = Instance.new("Folder")
	Folder.Name = "Inventory"
	Folder.Parent = player
	
	local Value = Instance.new("StringValue")
	Value.Name = "EquippedTool"
	Value.Value = "Red" -- testing purposes cause idk your join system
	Value.Parent = Folder
end)

A local script inside a ScreenGui in StarterGui:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remotes = replicatedStorage:FindFirstChild("Remotes")

--[[ Do not try to access StarterGui, thats a Server GUI that will be given to all player when join 
Just find the GUI you want to edit inside player/client ]]
--local kelenko = game:WaitForChild("StarterGui"):FindFirstChild("CurrencyGui"):FindFirstChild("kelenko")
local kelenko = script.Parent:WaitForChild("kelenko") -- Script is inside client, script is a child of gui

--[[ Client doesnt need access to ToolConfig module ]]
--local toolConfig = require(replicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))

-- Update kelenko text when receive signal from Server:
remotes.TextChange.OnClientEvent:Connect(function(strength)
	warn("Client got data from activating a tool:", strength)
	kelenko.Visible = true
	kelenko.Text = tostring(strength)
	
	-- No need to check value again, or to iterate toolConfig table again, sever already knows the data and its received by this client script. data is secure by server
	--[[
	local playerTool = player.Inventory.EquippedTool.Value
	for tool, toolTable in pairs(toolConfig) do
		if tool == playerTool then	
			kelenko.Visible = true
			kelenko.Text = toolTable.Strenght
		end
	end	
	]]
end)

I suggest rebuild mechanics of the client server communication.
Use this file for testing I copyied your scripts, edited, and you can check output:
Tools.rbxl (45.7 KB)

1 Like

OnClientEvent will make a connection to send Data from the Server to the Client
OnServerEvent will make a connection send Data from the Client to the Server

So if you have something that runs in a different context Level (Server an Client), You can use RemoteEvents to send them to the other and have that code run under that Context Level.

This can be useful as you may require a Client to do a Specific Task that can only be done on one Area but need another Part that has to be done within another which is the Server.

But, there are a couple of thing’s to note when Creating these Specific Events, Its seomthing that beginners tend to not look into for miss completely which is:

  • with OnServerEvent, you first argument is the Player
RemoteEvent.OnServerEvent:Connect(function(player) -- Player as First Argument
    print(player.Name) -- AngleNotLikeYou
end)
  • with OnClientEvent, there is no argument for the Player
RemoteEvent.OnClientEvent:Connect(function(player) -- no Player as first Argument
    print(player) -- nil or not nil
end)

when firing FireServer, it ignores the Player Argument from OnServerEvent and takes the Other arguments that follow
when firing FireClient, it requires the Player, This is because you are telling the coe to run for a Specific Client, if you want to run them for all Players, you can use FireAllClients for this purpose.

FireAllClients will only affect the Player who are currently in the Server as changes only happen on their Clients, when a Player joins, or when they leave and rejoin, those changes will not be there asd it only affected the Clients of those who were there when it fired.

There is a while Resource about RemoteEvents which I recommend you look into.

You can also look up YouTube tutorials for this if the Documentation is not on your Level.

1 Like

I really liked your script and tried it gone welldone Thank you . But only problem is
I got an error like this (I deleted your “Just test” leaderstats)


if you want to take a CloseLook

And Another thing is I can’t use that part because it doesn’t increase the Strenght value and I don’t know why it doesn’t change textlabels visible or number

and sorry for taking a hour to reply my cat Meowed so much so I take her to out “She is happy now”

Edit I misspelled strenght… lol

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