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

LocalScript mouse text works in Studio, but not in-game?

Asked by
traigla 75
8 years ago

I have this script that put a text label next to the mouse as you move it around, if you hover over a player, it shows their player name next to the mouse and when you don't hover over a player, it shows nothing. This script works in Studio on test mode and the test server feature, but it doesn't work in-game roblox. Any suggestions?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local debounce = false
local target = nil
local playerFound = false
local playerName = nil

mouse.Move:connect(function()
    target = mouse.Target
    if not target --[[or not (target.Parent:findFirstChild("Humanoid"))]] then
        if player.PlayerGui:findFirstChild("MouseText") then
            player.PlayerGui.MouseText.TextLabel.Text = ""
        end
    else
        if not (player.PlayerGui:findFirstChild("MouseText")) then
            local mouseGui = Instance.new("ScreenGui",player.PlayerGui)
            mouseGui.Name = "MouseText"
            local mouseText = Instance.new("TextLabel",mouseGui)
            mouseText.Size = UDim2.new(0,10,0,10)
            mouseText.BackgroundTransparency = 1
            mouseText.TextXAlignment = "Left"
            mouseText.TextStrokeTransparency = 0
            mouseText.TextColor3 = Color3.new(255,255,255)
        end
        if player.PlayerGui:findFirstChild("MouseText") then
            if target.Parent:IsA("Hat") and target.Parent.Parent:findFirstChild("Humanoid") then
                if target.Parent:findFirstChild("Humanoid") then
                    player.PlayerGui.MouseText.TextLabel.Text = target.Parent.Parent.Name
                    playerFound = true
                    playerName = target.Parent.Parent.Name
                else
                    player.PlayerGui.MouseText.TextLabel.Text = ""
                    playerFound = false
                end
            else
                if target.Parent:findFirstChild("Humanoid") then
                    player.PlayerGui.MouseText.TextLabel.Text = target.Parent.Name
                    playerFound = true
                    playerName = target.Parent.Name
                else
                    player.PlayerGui.MouseText.TextLabel.Text = ""
                    playerFound = false
                end
            end
            mouse.Button1Down:connect(function()
                if playerFound == true then
                    if not (player.PlayerGui:findFirstChild("PlayerSelectOption")) then
                        local newOptionGUI = script.Parent.PlayerSelectOption:clone()
                        newOptionGUI.Main.PlayerName.Value = playerName
                        newOptionGUI.Main.Script.Disabled = false
                        newOptionGUI.Parent = player.PlayerGui
                        newOptionGUI.Main.Visible = true
                    end
                    player.PlayerGui.ServeSystem.Serve.Title.Text = "Serving Player: "..playerName
                    player.PlayerGui.ServeSystem.Serve.PlayerName.Value = playerName
                end
            end)
            player.PlayerGui.MouseText.TextLabel.Position = UDim2.new(0,mouse.X+10,0,mouse.Y-5)
        end
    end
end)

1 answer

Log in to vote
0
Answered by 8 years ago

Problem

The only problem I can see is that since it works in studio and not in server it should be a latency problem, this happens because in studio everything is pretty much local so you don't have to wait for a server to create stuff so your local scripts run instantly. In server this doesn't happen as when a local scripts cause upons something in player or character that is server-sided then the local script is "local" which makes it run faster then the server can respond.

Solution

repeat wait() until game.Players.LocalPlayer.Character--To the top of every local script
Ad

Answer this question