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

Why is this RemoteEvent using Player as a parameter when I give it a string as an argument?

Asked by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

Output:

sub is not a valid member of Player

Hierarchy:

Workspace
    HoloScript (Script)
        CustomChatScript (LocalScript)
        Chatted (RemoteEvent)

I'll include all the code that I think you will need to help me solve this problem. Let me know if you need more. I apologize for all the reading.

In the past, I used the Roblox Chatted event to fire commands, but since some custom chat systems remove the Roblox chat bar, I had to make some changes. What I don't understand is why I'm getting this error, since I'm never giving an argument equal to Player.

Holo script:

local cmd = "start"
local cmdNum = cmd:len()
local customChat = true

function onChat(msg)
    if msg:sub(1,cmdNum) == cmd then --This is the line that's causing the error 
    --stuff
    end
end

game.Players.PlayerAdded:connect(function (player)

    repeat wait() until player.Character --Helps prevent glitching

    if isAdmin(player) then --Defined earlier; works fine.
        if customChat then
            script.Chatted.OnServerEvent:connect(onChat)

            local clone = script.CustomChatScript:Clone()
            clone.Parent = player:WaitForChild("PlayerGui")

            player.CharacterAdded:connect(function(chr) 
                local clone = script.CustomChatScript:Clone()
                clone.Parent = player:WaitForChild("PlayerGui")
            end)

        else --Otherwise,
            player.Chatted:connect(onChat) 
        end
    end
end)

Here's the LocalScript named CustomChatScript, located in workspace.HoloScript:

local plr = game.Players.LocalPlayer
    repeat wait() until plr.Character
local chr = plr.Character
local mouse = plr:GetMouse()

local startCommand = "/" 

local addToString = false
local theString = ""

game.StarterGui:SetCoreGuiEnabled("Chat", false)

mouse.KeyUp:connect(function(key)
    if key == startCommand and not addToString then
        addToString = true 
        chr:WaitForChild("Humanoid").WalkSpeed = 0
    elseif key:byte() == 13 and addToString then --13 = "Enter"
        workspace:WaitForChild("HoloScript").Chatted:FireServer(theString)--See? I'm not feeding in a player...
        theString = ""
        addToString = false
        chr:WaitForChild("Humanoid").WalkSpeed = 16
    elseif key:byte() ~= 13 and addToString then
        theString = theString..key
    end
end)

1 answer

Log in to vote
2
Answered by 10 years ago

The events OnServerEvent or OnServerInvoke both have the Player who triggered the event as the first parameter, always. The string that you passed to the event will be the second parameter.

0
How do you propose I fix that? If I just add another parameter to the function, it will error when the normal Chatted event is connected to it, since the regular Chatted event has Message as the first parameter. Perci1 4988 — 10y
Ad

Answer this question