Zuher Laith

attempt to index global 'Player' (a nil value)

Recommended Posts

Hello everyone,

I'm working on a resource that sends a dx message to the player on both server/client side and gets called from other resources .

this is the full code:

Spoiler

server.lua


function dxOutputMessage(message, player, r, g, b) -- this works for server side trigger from another resource
	triggerClientEvent(player, "dxMessage.dxOutputMessage", player, message, r, g, b);
end

function outputMessage(message, player, r, g, b)
	dxOutputMessage(message, player, r, g, b);
end

function Player:outputMessage(message, r, g, b) -- error here
	dxOutputMessage(message, self, r, g, b);
end

addEvent("dxMessage.outputMessage", true);
addEventHandler("dxMessage.outputMessage", root, function(message, r, g, b)
	source:outputMessage(message, r, g, b); -- and here
end);

client.lua:


function outputMessage(message, r, g, b)
	triggerServerEvent("dxMessage.outputMessage", localPlayer, message, r, g, b);
end


dxoutput.client.lua;
 


local screen_width, screen_height =  guiGetScreenSize();
local relative_width, relative_height = screen_width / 1440, screen_height / 900

local static_left = relative_width * 983;
local cur_left = static_left;
local move_offet = relative_width * 20;
local cur_message = "";
local cur_color = {255, 255, 255};
local message_moving = false;
local message_moving_back = false;
local message_moving_back_timer;

addEventHandler("onClientRender", root, function()
	dxDrawFramedText(cur_message, cur_left, relative_height * 618.0, cur_left + (relative_width * 456), relative_height * 645.0, tocolor(cur_color[1], cur_color[2], cur_color[3], 255), 1.0, "default-bold", "center", "center", false, true, true);
end);

function dxOutputMessage(message, r, g, b)
	if (message_moving) then removeEventHandler("onClientRender", root, moveMessage); end
	if (message_moving_back) then removeEventHandler("onClientRender", root, moveMessageBack); end
	if (message_moving_back_timer and message_moving_back_timer:isValid()) then message_moving_back_timer:destroy(); end
	cur_left, cur_message, cur_color = relative_width * 1440, message or "", {r or 255, g or 255, b or 255};
	message_moving_back_timer = setTimer(hideMessage, 5000, 1);
	addEventHandler("onClientRender", root, moveMessage);
	message_moving = true;
end

function moveMessage()
	cur_left = cur_left - move_offet;
	if (cur_left <= static_left) then
		removeEventHandler("onClientRender", root, moveMessage);
		cur_left = relative_width * 983;
		message_moving = false;
	end
end

function hideMessage()
	message_moving_back = true;
	addEventHandler("onClientRender", root, moveMessageBack);
end

function moveMessageBack()
	cur_left = cur_left + move_offet;
	if (cur_left >= relative_width * 1440) then
		removeEventHandler("onClientRender", root, moveMessageBack);
		cur_left = relative_width * 1440;
		message_moving_back = false;
	end
end
	
function dxDrawFramedText(message, left, top, width, height, color, scale, font, alignX, alignY, clip, wordBreak, postGUI)
	dxDrawText(message, left + 1, top + 1, width + 1, height + 1, tocolor(0, 0, 0, 255), scale, font, alignX, alignY, clip, wordBreak, postGUI);
	dxDrawText(message, left + 1, top - 1, width + 1, height - 1, tocolor(0, 0, 0, 255), scale, font, alignX, alignY, clip, wordBreak, postGUI);
	dxDrawText(message, left - 1, top + 1, width - 1, height + 1, tocolor(0, 0, 0, 255), scale, font, alignX, alignY, clip, wordBreak, postGUI);
	dxDrawText(message, left - 1, top - 1, width - 1, height - 1, tocolor(0, 0, 0, 255), scale, font, alignX, alignY, clip, wordBreak, postGUI);
	dxDrawText(message, left, top, width, height, color, scale, font, alignX, alignY, clip, wordBreak, postGUI);
end

addEvent("dxMessage.dxOutputMessage", true)
addEventHandler("dxMessage.dxOutputMessage", root, function(message, r, g, b)
	dxOutputMessage(message, r, g, b);
end);

meta:


<meta>
	<info name = "Dx Message Output" version = "1.0" type = "script" />

	<script src = "server.lua" type = "server" />
	<script src = "client.lua" type = "client" />
	<script src = "dxoutput.client.lua" type = "client" />

	<export function="outputMessage" type="client"/>
	<export function="outputMessage" type="server"/>
	<export function="dxOutputMessage" type="server"/>
	<export function="Player:outputMessage" type="server"/>
</meta>

 


So in case I want to call and pass a message on server side from another resource, I use this:

Quote

triggerClientEvent(thePlayer, "dxMessage.dxOutputMessage", thePlayer, "Message passed ! !", 0, 255, 0)


and If I wanted to pass it on client side, I use "call" function:

Quote

call(getResourceFromName("[add]DxMessage"), "outputMessage", "Message passed !", 0, 255, 0)


this is the first error I got after debugging:

Quote

ERROR: [add]DxMessage\server.lua:9: attempt to index global 'Player' (a nil value)


I know its a bit messy,
Any help is appreciated .

Edited by Zuher Laith

Share this post


Link to post
function dxOutputMessage(message, player, r, g, b) -- this works for server side trigger from another resource
	triggerClientEvent(player, "dxMessage.dxOutputMessage", player, message, r, g, b);
end

function outputMessage(message, player, r, g, b)
	dxOutputMessage(message, player, r, g, b);
end

addEvent("dxMessage.outputMessage", true);
addEventHandler("dxMessage.outputMessage", root, function(message, r, g, b)
	outputMessage(message, client, r, g, b); -- and here
end);

 

 

It is not recommended to mix oop syntax in your code, unless you want to learn that,

Share this post


Link to post
1 hour ago, IIYAMA said:

It is not recommended to mix oop syntax in your code, unless you want to learn that,

I guess yeah,
it worked but after triggering again, an error pop out on dxoutput.client.lua on this at line 5:

local message_moving_back_timer;
function dxOutputMessage(message, r, g, b)
	if (message_moving) then removeEventHandler("onClientRender", root, moveMessage); end
	if (message_moving_back) then removeEventHandler("onClientRender", root, moveMessageBack); end
	if (message_moving_back_timer and message_moving_back_timer:isValid()) then message_moving_back_timer:destroy(); end -- message_moving_back_timer
	cur_left, cur_message, cur_color = relative_width * 1440, message or "", {r or 255, g or 255, b or 255};
	message_moving_back_timer = setTimer(hideMessage, 5000, 1);
	addEventHandler("onClientRender", root, moveMessage);
	message_moving = true;
end

the error I got:

Quote

ERROR: Attempt to index upvalue 'message_moving_back_timer' (a userdata value)

 

Share this post


Link to post

Put this in your meta.xml

<oop>true</oop>

 

As I said before, do not mix oop in your script, unless you know what you are doing.

 

  • Thanks 1

Share this post


Link to post
2 hours ago, IIYAMA said:

As I said before, do not mix oop in your script, unless you know what you are doing.

Well, let me be honest with you, I wasn't the one who wrote this code, that's how it became difficult to me to understand it.
I've just read about OOP and getting rid of it might requires re-writing the whole resource, correct ?

Share this post


Link to post

Re-writting it in your own style is a good way of learning how the code works.

  • Like 1

Share this post


Link to post
6 minutes ago, IIYAMA said:

Re-writting it in your own style is a good way of learning how the code works.

Good Advice :love9: I'll work with it, Thanks .

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.