✓ Accepted answer
##Problem
Seeing as your code is located in Workspace, I am assuming this is a server script. In which case, ServerScripts can not access LocalPlayer. This is one of the more common mistakes I have cited in my blog article.
##Solution
There are two ways of going about this.
- Using a LocalScript
- You can rename the part in Workspace to something unique. Whenever the character hits the part then the GUI elements can change. This is actually recommended as you're dealing with GUI elements which should be handled by the clients. This method is also compatible with FilteringEnabled environments.
- Using a ServerScript
- You can keep the script in the normal location and gather the player from the GetPlayerFromCharacter method. Once gathering the player you can go into the PlayerGui and modify the text.
##Final Script
####Solution 1 (LocalScript Method):
LocalScripts will only work in PlayerGui, a Player's Backpack, PlayerScripts, or the Player's Character.
game.Workspace.PartNameHere.Touched:Connect(function(partThatTouched)
if partThatTouched.Parent == game.Players.LocalPlayer.Character then
game.Players.LocalPlayer.PlayerGui.PlaceGui.MainFrame.TextShower.Text = "Testing"
end
end)
####Solution 2 (ServerScript Method):
script.Parent.Touched:Connect(function(partThatHit)
local player = game.Players:GetPlayerFromCharacter(partThatHit.Parent)
if player then
player.PlayerGui.PlaceGui.MainFrame.TextShower.Text = "Testing"
end
end)
######Hopefully this answered your question. If it did, hit the accept answer button. If you have any further questions, post them in the comments below and I will get back to you.
5 comments
game.Players.LocalPlayercan only be accessed from aLocalScript. It's nil inside of a Script."Touched" is an event not a function.
^
Is this a local script
Next time use the code block feature. It's the Lua Button