Error from making a function serverside and using it on client side
7 replies, posted
I'm sorry if this sounds dumb, but I'm having a problem creating a function on server side and trying to use it on client side, and I'm getting an error from it.
[CODE]
if SERVER then
util.AddNetworkString("YoutubeMSG")
hook.Add("PlayerSay", "PlayerSayExample", function(ply, text, team)
if (string.sub(text, 1, 8) == "/playnow") then
local DankMemes = string.sub(text, 9)
net.Start("YoutubeMSG")
dankMemes = string.sub(text, 9)
net.Send(ply)
return "Now Playing: " .. string.sub(text, 9)
end
end)
else
net.Receive("YoutubeMSG", function()
local frame = vgui.Create("DFrame")
frame:SetTitle("youtube")
frame:SetSize(ScrW() * 0.75, ScrH() * 0.75)
frame:Center()
frame:MakePopup()
local html = vgui.Create("HTML", frame)
html:Dock(FILL)
html:OpenURL(dankMemes())
end)
end
[/CODE]
The error is:
[CODE]
[ERROR] addons/player/lua/autorun/youtubeplayer.lua:26: attempt to call global 'dankMemes' (a nil value)
1. func - addons/player/lua/autorun/youtubeplayer.lua:26
2. unknown - lua/includes/extensions/net.lua:32
[/CODE]
You're not using the net library correctly. Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/net/WriteString]net.WriteString[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/net/ReadString]net.ReadString[/url].
[CODE]
if SERVER then
util.AddNetworkString( "YoutubeMSG" )
hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team )
if string.sub( text, 1, 8 ) == "/playnow" then
local DankMemes = string.sub( text, 10 )
net.Start( "YoutubeMSG" )
net.WriteString( DankMemes )
net.Send( ply )
return "Now Playing: " .. DankMemes
end
end )
else
net.Receive( "YoutubeMSG", function()
local frame = vgui.Create( "DFrame" )
frame:SetTitle( "YouTube" )
frame:SetSize( ScrW() * 0.75, ScrH() * 0.75 )
frame:Center()
frame:MakePopup()
local html = vgui.Create( "HTML", frame )
html:Dock( FILL )
html:OpenURL( net.ReadString() )
end )
end
[/CODE]
[editline]12th November 2017[/editline]
Also, you don't need to use the net library at all. Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/OnPlayerChat]GM:OnPlayerChat[/url].
[CODE]
hook.Add( "OnPlayerChat", "YouTubeMSG", function( ply, text, team, dead )
if ply ~= LocalPlayer() then return end
if string.sub( text, 1, 8 ) == "/playnow" then
local frame = vgui.Create( "DFrame" )
frame:SetTitle( "YouTube" )
frame:SetSize( ScrW() * 0.75, ScrH() * 0.75 )
frame:Center()
frame:MakePopup()
local html = vgui.Create( "HTML", frame )
html:Dock( FILL )
html:OpenURL( string.sub( text, 10 ) )
return true
end
end )
[/CODE]
The openURL isn't able to open the url because it's not encased in "" I'm pretty sure.
[QUOTE=water1108;52881194]The openURL isn't able to open the url because it's not encased in "" I'm pretty sure.[/QUOTE]
You don't need to encase a string that's already a string in quotes, E.G. this doesn't work for obvious reasons:
[CODE]
local str = "this is already in quotes"
print("str")
[/CODE]
Using that code, it says [General] The command 'playnow' is not a valid command.
in chat
[QUOTE=water1108;52881202]Using that code, it says [General] The command 'playnow' is not a valid command.
in chat[/QUOTE]
Sorry, found the problem.
[CODE]
local DankMemes = string.sub( text, 9 )
[/CODE]
Should be
[CODE]
local DankMemes = string.sub( text, 10 )
[/CODE]
Because there's a space between the command and the actual website link.
I updated my posts so they should both work now.
[EDITLINE]12 November[/EDITLINE]
By the way, by default, Garry's Mod never tells you whether or not your chat commands are valid.
This is because by default Garry's Mod has no concept or library defining what a chat command is.
It seems you have some sort of command addon that may be interfering with this code.
If this addon has a different way of adding a command, you should probably use it to avoid problems.
Snip - dumb comment miss interpreted what u said xD
Yeah, I'm using the Skill Surf gamemode from flow and it doesn't let me use client side commands, but thank you the serverside fix worked.
:snip: automerge again lol sorry
Sorry, you need to Log In to post a reply to this thread.