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

Script that shows a Text Button when im standing on a part doesnt work, no errors or outputs?

Asked by 4 years ago

Hello, im trying to make a script in which when you stand on top of a part it makes a text button appear. Its a local script and the directory of it is Workspace\Part\LocalScript, and the directory of the text button is StarterGui\ScreenGui\TextButton. Here is the code:

local player = game.Players.LocalPlayer

local function Touched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        player.PlayerGui.ScreenGui.TextButton.Visible = true
    end
end

script.Parent.Touched:Connect(Touched)
0
have you tried debugging it by adding a print to every thread? misha123 83 — 4y
0
yes, i have but no prints work, not even before the local player variable Phantom_User101 17 — 4y

1 answer

Log in to vote
0
Answered by
Memotag 226 Moderation Voter
4 years ago
Edited 4 years ago

You've already got the basics in place but your issue is stemming from determining whether the part that touched the other part is a member of a Characters model.

local player = game.Players.LocalPlayer will only work when using a LocalScript and in this instance, you are using a ServerScript so this should be changed to just local Players = game.Players.

To fix this, you can use the GetPlayerFromCharacter method like this

local player = Players:GetPlayerFromCharacter(hit.Parent)

After which you can check whether the method returned a Player.

if player ~= nil then

end

Putting it altogether will get you:

local Players = game.Players

local function Touched(hit)
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    if player ~= nil then
        player.PlayerGui.ScreenGui.TextButton.Visible = true
    end
end

script.Parent.Touched:Connect(Touched)

0
Thank you for the help! Phantom_User101 17 — 4y
0
No problem! Memotag 226 — 4y
Ad

Answer this question