Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to change TextLabel text by being in contact with a certain block?

Asked by 6 years ago
Edited 6 years ago

Hello,

In an obby I am making, whenever a player gets to a new stage, or touches the spawn point of the next stage, a checkpoint is set and the GUI text for a TextLabel is supposed to be changed to the stage that they are on.

I tried to use this:

script.Parent.Touched:connect(function(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then

    game.StarterGui.ScreenGui.TextLabel.Text = "STAGE: 2"

    end

end)

But it only changed when the player died. Any help? I am a beginner at scripting and may not understand the answer.

0
You can either: (1) Have a localscript deal with the Touched event of the part and change the TextLabel color via PlayerGui, although you wouldn't be able to put it in workspace (you wouldn't be able to use script.Parent) or (2) Fire a remove event to the player which a localscript then changes the TextLabel color in the player's PlayerGui Vulkarin 581 — 6y
0
Would I have to put the LocalScript in StarterPlayerScripts? Kaexotix 57 — 6y
0
Localscripts can run in a lot of places, see the bottom link of gioni01's answer for that. He turned what I said into an answer so that should help you Vulkarin 581 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

The issue is that you're changing the Gui object in StarterGui. If this is a server script, your script is working like it's supposed to, however, you're not seeing the change because the contents of StarterGui are not cloned into the PlayerGui folder until the character reloads. To solve this issue, you can either...

  • Use a remote event

  • Use a local script

If you use a local script, it cannot be located in the workspace. You'll have to put it somewhere that is a direct descendant of the player, like StarterPlayerScripts, StarterGui, or StarterPack. This is an example with the local script:

--Local script located in StarterPlayerScripts

local player = game:GetService("Players").LocalPlayer
local Button = workspace:WaitForChild("Button")

Button.Touched:Connect(function(part)
   if part.Parent == player.Character then
      player.PlayerGui.ScreenGui.TextLabel.Text = "STAGE: 2"
   end
end)

Let's try this the other way as well:

--Server script in ServerScriptService

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Button = workspace:WaitForChild("Button")
local Remote = Instance.new("RemoteEvent")
Remote.Parent = ReplicatedStorage

Button.Touched:Connect(function(part)
   local player = Players:GetPlayerFromCharacter(part.Parent)
   if player then
      Remote:FireClient(player,"") --you can send any data in the second argument
   end
end)

--Local script in StarterPlayerScripts

local player = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("RemoteEvent") --wait for remote

Remote.OnClientEvent:Connect(function(args) --the data you sent
   player.PlayerGui.ScreenGui.TextLabel.Text = "STAGE: 2"
end)

Resources:

Remote Events

:GetPlayerFromCharacter()

Server Script

Local Script

Accept and upvote if this helps!

0
he doesn't have enough rep to upvote lol User#23365 30 — 6y
0
Thank you so much, it works! Kaexotix 57 — 6y
0
accept the answer pls Gey4Jesus69 2705 — 6y
Ad

Answer this question