How to make a text change when player touch an object

||local plr = playerService:GetPlayerFromCharacter(char)|
||local text = plr.PlayerGui.mainGui.Frame.TextLabel.Text|
||text.Value = plr.PlayerGui.mainGui.Frame.TextLabel.Text.Value + 1|
||text.Text = text.Value|

my script wont work, there is this error

Workspace.crystal.Script:9: attempt to index nil with 'PlayerGui'

Script:9 is the second line of my code that im showing
how should i fix?

1 Like

Is this a server script or a local script? If this is a server script, just make an if statement to make sure the player exists before running the rest of the code.

local plr = playerService:GetPlayerFromCharacter(char)
if plr then
	-- rest of the code
end

If this doesn’t fix the issue, your ‘char’ variable might be nil.

it work fixing the error, but the part which change the value dosent work. the value didnt even change. do u know how to fix?

Is ‘text’ a TextLabel or a Value? I believe you are mentioning the TextLabel’s property ‘Text’ as the variable instead of the instance. And where is your Value located?

a Value. A number value which is located under the text label

nope still not working, the value still dosent change

I recommend if you’re working with GUI, you use LocalScript.

For example -

part.Touched:Connect(function()
--rest of the code.
end)
1 Like

there is no “value” of a text. you need to say …Text = …Text + 1
but your error isn’t related to that. You are not giving a correct argument to function in first line, check what char is.

no there is a value called value as a child of the textlabel

if it is the child of textlabel why did you write TextLabel.Text.Value (this means its the child of text???)

and why do you keep an extra variable called value if you are eventually going to set text equal to value?

nope no error. the value name is called NumValue

local Crystal = script.Crystal
local playerService = game:GetService("Players")

script.Crystal.Touched:Connect(function(char)

	Crystal.Transparency = 1
	Crystal.CanTouch = false
	local plr = playerService:GetPlayerFromCharacter(char)
	if plr then
		local plr = playerService:GetPlayerFromCharacter(char)
		local text = plr.PlayerGui.mainGui.Frame.TextLabel
		local value = text:FindFirstChild("NumValue")
		value.Value += 1
		text.Text = value.Value
	end
	wait(5)

	Crystal.Transparency = 0
	Crystal.CanTouch = true

end)

just incase i missed something important heres my whole script

You should go client-side though so you don’t need to use :GetPlayerFromCharacter() method. You could just use LocalPlayer’s character (client’s character) and change the GUI.

the parameter inside touched is the hit location not character itself, you need to set char = char.Parent inside the function

1 Like

Let me recorrect what you wanna convey -

local Crystal = script.Crystal
local playerService = game:GetService("Players")

script.Crystal.Touched:Connect(function(char)

	Crystal.Transparency = 1
	Crystal.CanTouch = false
	local plr = playerService:GetPlayerFromCharacter(char.Parent)
	if plr then
		local text = plr.PlayerGui.mainGui.Frame.TextLabel
		local value = text:FindFirstChild("NumValue")
		value.Value += 1
		text.Text = value.Value
	end
	wait(5)

	Crystal.Transparency = 0
	Crystal.CanTouch = true

end)
1 Like

Your above post says

if not hitPart.Part.Parent:FindFirstChild("Humanoid") then

which isn’t correct. There isn’t any other part. I don’t know whether it’s a typo or not, but also, he thought I solved his actual problem, yeah you do told him some more things to be fixed I guess but solution is just when your actual problem is solved, rest all more things given are good but actual problem should be solved. And you solved this problem, but when he marked as a solution, but normally we consider people giving answers first. I just recorrected @LeqenDzTR post, like doing some minor changes, but it’s his choice what he got worked. It’s just your posts was good for some extra issues, you picked up the main thing, but it was after he marked as solution, also we shouldn’t focus on solutions, we should focus on helping people which you did, indeed. Anyways, thanks.

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