Hi! I need some help. I want to update a TextLabel’s text to be a value’s number, but for some reason, it never changes the TextLabel.
I have tried putting the code in different places and not using tostring(), but nothing works.
Code:
local Players = game:GetService("Players")
local corn = script.Parent
local cornamount = game:GetService("StarterGui").cornamount.Frame.cornamountlabel
local cornamountval = game:GetService("ReplicatedStorage").cornamount
script.Parent.Touched:Connect(function(otherpart)
local partparent = otherpart.Parent
local humanoid = partparent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
corn:Destroy()
cornamountval.Value = cornamountval.Value - 1
local player = Players:GetPlayerFromCharacter(partparent)
local leaderstats = player.leaderstats
local cornStat = leaderstats and leaderstats:FindFirstChild("corn")
if cornStat then
cornStat.Value = cornStat.Value + 10
end
cornamount.Text = tostring(cornStat.Value)
end
end)
Thanks!
3 Likes
Your problem is actually really common and happens a lot to new programmers. You’re attempting to update the TextLabel’s text to the StarterGui (as opposed to the PlayerGui.) When you make changes to the StarterGui, players won’t be able to see those changes until they reset.
To get around this you need to directly reference the LocalPlayer and modify the gui through Player.PlayerGui.
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local corn = script.Parent
local cornamount = PlayerGui.cornamount.Frame.cornamountlabel
local cornamountval = game:GetService("ReplicatedStorage").cornamount
script.Parent.Touched:Connect(function(otherpart)
local partparent = otherpart.Parent
local humanoid = partparent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
corn:Destroy()
cornamountval.Value = cornamountval.Value - 1
local player = Players:GetPlayerFromCharacter(partparent)
local leaderstats = player.leaderstats
local cornStat = leaderstats and leaderstats:FindFirstChild("corn")
if cornStat then
cornStat.Value = cornStat.Value + 10
end
cornamount.Text = tostring(cornStat.Value)
end
end)
3 Likes
Small problem, i’m using a normal script.
You should be using LocalScript’s when working with user interface. Player data, in this case, corn, should be incremented and fetched through the server using RemoteEvents or RemoteFunctions.
Here’s a link to a helpful resource on the Roblox documentation page for more information about the client-server boundary on Roblox.
2 Likes
local Players = game:GetService("Players")
local corn = script.Parent
local cornamountval = game:GetService("ReplicatedStorage").cornamount
script.Parent.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
corn:Destroy()
cornamountval.Value = cornamountval.Value - 1
local leaderstats = player.leaderstats
local cornStat = leaderstats and leaderstats:FindFirstChild("corn")
if cornStat then
cornStat.Value = cornStat.Value + 10
end
player.cornamount.Frame.cornamountLabel.Text = tostring(cornStat.Value)
end
end)
1 Like
Nope, doesn’t change the textlabel. (unless this is for a localscript)
When I put this into a localscript, it doesn’t destroy the corn.
I’ll read the articles, so let’s see how that goes
Helped to shorten my code (beside from the tostring() line)
The article helped me understand RemoteEvents, and it actually worked! Thank you both so much!
New code:
local Players = game:GetService("Players")
local corn = script.Parent
--Basically what happens if the corn is touched
local RepStorage = game:GetService("ReplicatedStorage")
local cornamountval = RepStorage:WaitForChild("cornamount")
local cornamount = game.StarterGui.cornamount.Frame:WaitForChild("cornamountlabel")
local CornHarvestEvent = RepStorage:WaitForChild("CornHarvestEvent")
corn.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
corn:Destroy()
cornamountval.Value = cornamountval.Value - 1
local leaderstats = player.leaderstats
local cornStat = leaderstats and leaderstats:FindFirstChild("corn")
if cornStat then
cornStat.Value = cornStat.Value + 10
end
CornHarvestEvent:FireClient(player,cornStat.Value)
end
end)
--Changes the textlabel
local cornamount = script.Parent.cornamount.Frame:WaitForChild("cornamountlabel")
local RepStorage = game:GetService("ReplicatedStorage")
local CornHarvestEvent = RepStorage:FindFirstChild("CornHarvestEvent")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local leaderstats = LocalPlayer.leaderstats
local cornStat = leaderstats and leaderstats:FindFirstChild("corn")
CornHarvestEvent.OnClientEvent:Connect(function(player)
cornamount.Text = tostring(cornStat.Value)
end)
Small problem, it only works when the player touches a piece of corn. When the player joins, the label is just 0. It only changes when the player gets a piece of corn
@OppaStoppaStomppa
@CommanderRanking
1 Like