Jump to content

How to shorten a string?


Recommended Posts

One way is to split it in letters then create a new variable which will contain the four chars in a string

The function is here https://wiki.multitheftauto.com/wiki/RemoveHex

NOT CREATED BY ME

I think you need to read his question again.

RemoveHex(created By me) : This function is used to remove hex color codes from strings.

Hello, I want to shorten a string like, if one string (nickname from player) is too long, shorten it.

Like this:

-> original string: Nickname

-> remove it untill it is only 4 length.

-> returns Nick

All what you need string.len

if (string.len (name) > 4) then 
    -- change the player name  
end 

For more information about strings check my tuto Lua Strings

Link to comment

Generally to shorten a string, you should use string.sub

e.g:

  
local myString = "Nickname" 
  
outputChatBox( string.sub(myString, 1, 4) ) -- Returns 'Nick' 
  

Number 1 in there indicates where you want to shorten the string from, in case it's from N ( Nickname )

and Number 4 indicates where to you want to shorten the string, in this case to k ( Nickname )

Link to comment
One way is to split it in letters then create a new variable which will contain the four chars in a string

The function is here https://wiki.multitheftauto.com/wiki/RemoveHex

NOT CREATED BY ME

I think you need to read his question again.

RemoveHex(created By me) : This function is used to remove hex color codes from strings.

Hello, I want to shorten a string like, if one string (nickname from player) is too long, shorten it.

Like this:

-> original string: Nickname

-> remove it untill it is only 4 length.

-> returns Nick

All what you need string.len

if (string.len (name) > 4) then 
    -- change the player name  
end 

For more information about strings check my tuto Lua Strings

removeHex, isn't removing the Hexs Today

These Days the only Solution to remove hexs, is to remove the Color of The Player When he chats.

Link to comment
removeHex, isn't removing the Hexs Today

These Days the only Solution to remove hexs, is to remove the Color of The Player When he chats.

Really lol ? simply to prevent players from using hex codes you can use it on:

onPlayerJoin 
onPlayerChangeNick 

Link to comment

if you need to remove the colors in chat and in server use this

addEventHandler ( "onPlayerConnect", getRootElement(),function(nick) 
     if(string.find(nick,"#%x%x%x%x%x%x"))then 
          cancelEvent(true,"Remove your name colors for join the IP") 
     end 
end) 
  
addEventHandler ( "onPlayerConnect", getRootElement(),function(nick) 
     if(string.find(nick,"##%x%x%x%x%x%x"))then 
          cancelEvent(true,"Remove your name colors for join the IP") 
     end 
end) 
  
addEventHandler ( "onPlayerChat", getRootElement(),function(nick) 
     if(string.find(nick,"#%x%x%x%x%x%x"))then 
kickPlayer(source, getRootElement(), "Do not use colors in Chat!") 
     end 
end) 
  
addEventHandler ( "onPlayerChat", getRootElement(),function(nick) 
     if(string.find(nick,"##%x%x%x%x%x%x"))then 
kickPlayer(source, getRootElement(), "Do not use colors in Chat!") 
     end 
end)  

Link to comment

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.
×
×
  • Create New...