JehadGh

a Problem in a simple code

Recommended Posts

hello guys

first things first i'm a beginner so i have a problem in a simple code that i tried too many times , the error that when i type /data i get "you are a guest" even if i have logged in my account

here is the code

server

local guests = { }
local players = { }

function join()
setElementData( source, "guests", 1)
end

addEventHandler("onPlayerJoin", getRootElement(), join)

function login()
setElementData( source, "players", 1)
end

addEventHandler("onPlayerLogin", getRootElement(), login)

client

function check()
if  (getElementData( localPlayer, "guests") == 1 ) then
     outputChatBox("You are a guest")
else if (getElementData( localPlayer, "players") == 1 ) then	 
     outputChatBox("You are a player")
end
end
end
addCommandHandler("data", check)

i have tried to do it in the client only and its worked, but i want to know how the data work

again i'm know i'm a beginner but in my language there is no good courses in lua so i don't understand everything , thanks

Edited by JehadGh

Share this post


Link to post

When player logged in, you don't set "guests" elementdata to false/nil.

So, (getElementData(localPlayer, "guests") == 1) always returns true. (==> don't check second condition)

You can solve it, if you set "guest" false/nil, when player logged in.

 

btw:

- "else if" is incorrect, the correct is "elseif"

- guest and players tables are unused, so you can remove it

- use /debugscript 3

 

One solution:

-- SERVER
function login()
	setElementData(source, "loggedin", true)
end
addEventHandler("onPlayerLogin", getRootElement(), login)
-- CLIENT
function check()
	if (getElementData(localPlayer, "loggedin")) then
		outputChatBox("You are a player")
	else
		outputChatBox("You are a guest")
	end
end
addCommandHandler("data", check)

 

Edited by stPatrick

Share this post


Link to post
2 minutes ago, JehadGh said:

so 1 means true and 0 means false?

No, 0 and 1 is Integer.

So, 1 means 1 and 0 means 0. :D

Edited by stPatrick

Share this post


Link to post
2 minutes ago, JehadGh said:

your so nice with me lol , i just tried the code and when i start the mod i get this

ERROR: Loading script failed , unexpected symbol near "

in this line addEventHandler("onPlayerLogin", getRootElement(), login)

 

In line 5, at the end have a invisible character, delete it.

Share this post


Link to post
13 minutes ago, JehadGh said:

sorry but one last question


-- CLIENT
1. function check()
2.	if (getElementData(localPlayer, "loggedin")) then
3.		outputChatBox("You are a player")
4.	else
5.		outputChatBox("You are a guest")
6.	end
7. end
8. addCommandHandler("data", check)

why we didn't type true in the line 2. like this


	if (getElementData(localPlayer, "loggedin") == true ) then

 

 

 

Because: true == true is true :D

(So, true == true and true are same.)

Edited by stPatrick

Share this post


Link to post
3 hours ago, JehadGh said:

sorry but one last question


-- CLIENT
1. function check()
2.	if (getElementData(localPlayer, "loggedin")) then
3.		outputChatBox("You are a player")
4.	else
5.		outputChatBox("You are a guest")
6.	end
7. end
8. addCommandHandler("data", check)

why we didn't type true in the line 2. like this


	if (getElementData(localPlayer, "loggedin") == true ) then

 

Cause 1 == 2 means false 

if true==true means true

if false==true means false

if false==false means true

when you type ( if true then )

you'll get true cause true means true :) 

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.