Jump to content

Some script isn't working at all.


Deltanic

Recommended Posts

When I look at my script, it looks fine. So I don't know the problem.

rootElement = getRootElement()
thisPlayer = getLocalPlayer()
 
local portalBool
local portalMarker1
local portalMarker2
 
addEventHandler("onClientPlayerJoin", rootElement,
function()
	portalBool = 0
	portalMarker1 = 0
	portalMarker2 = 0
end
)
 
addEventHandler("onClientPlayerQuit", rootElement,
function()
	portalBool = 0
	portalMarker1 = 0
	portalMarker2 = 0
destroyElement( portalMarker1 )
destroyElement( portalMarker2 )
end
)
 
addEventHandler( "onClientPlayerWeaponFire", thisPlayer, 
function( weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
if portalBool == 1 then
if weapon == 31 then
if not hitElement ==  "player" then
if getKeyState( "1" ) == true then
destroyElement( portalMarker1 )
					portalMarker1 = createMarker( hitX, hitY, hitZ, "corona", 4, 0, 125, 35, 200 )
end
if getKeyState( "2" ) == true then
destroyElement( portalMarker2 )
					portalMarker2 = createMarker( hitX, hitY, hitZ, "corona", 4, 180, 0, 0, 200 )
end
end
end
end			
end
)
 
addCommandHandler ( "portal",
function( commandName )
if portalBool == 0 then
		portalBool = 1
outputChatBox( "Portalgun turned on." )
end
if portalBool == 1 then
		portalBool = 0
destroyElement( portalMarker1 )
destroyElement( portalMarker2 )
outputChatBox( "Portalgun turned off." )
end
outputChatBox( "Command done." )
end
)

The problem I get is in the command. The only message I get is "Command done.", and nothing else. Why does the rest of the command not work?

Link to comment

first, replace "end if" with elseif, cause right now if portalBool is 0 you're setting it to 1 and then setting it back to 0:

addCommandHandler ( "portal",
function( commandName )
if portalBool == 0 then
        portalBool = 1
outputChatBox( "Portalgun turned on." )
elseif portalBool == 1 then
        portalBool = 0
if isElement(portalMarker1) then destroyElement(portalMarker1) end
if isElement(portalMarker2) then destroyElement(portalMarker2) end
outputChatBox( "Portalgun turned off." )
end
outputChatBox( "Command done." )
end
)

and i guess its better to check if element exists, before destroying it (otherwise you can get "Bad pointer" warnings in log).

if you want to remove variable, use "nil", because "0" is still a value:

addEventHandler("onClientPlayerQuit", rootElement,
function()
if isElement(portalMarker1) then destroyElement(portalMarker1) end
if isElement(portalMarker2) then destroyElement(portalMarker2) end
     portalBool = nil
     portalMarker1 = nil
     portalMarker2 = nil
end
)

and why don't you use boolean for portalBool?

addCommandHandler ( "portal",
function( commandName )
if portalBool then
        portalBool = false
if isElement(portalMarker1) then destroyElement(portalMarker1) end
if isElement(portalMarker2) then destroyElement(portalMarker2) end
outputChatBox( "Portalgun turned off." )
else
        portalBool = true
outputChatBox( "Portalgun turned on." )
end
outputChatBox( "Command done." )
end
)

one more thing, onClientPlayerWeaponFire returns an ELEMENT with hitElement, not it's type, so "if not hitElement == "player" then" won't work,

use getElementType:

addEventHandler( "onClientPlayerWeaponFire", thisPlayer,
function( weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement )
if (portalBool == 1) and (weapon == 31) and (getElementType(hitElement) ~= "player") then
if getKeyState( "1" ) then
if isElement(portalMarker1) then destroyElement(portalMarker1) end
             portalMarker1 = createMarker( hitX, hitY, hitZ, "corona", 4, 0, 125, 35, 200 )
elseif getKeyState( "2" ) then
if isElement(portalMarker2) then destroyElement(portalMarker2) end
             portalMarker2 = createMarker( hitX, hitY, hitZ, "corona", 4, 180, 0, 0, 200 )
end
end
end
)

Link to comment

Awesome, command is working now :D But there's still a problem.

Serverscript:

addEventHandler( "givePortalGun", getRootElement(),
function( )
giveWeapon( source, 31, 1100 )
end
)
addEvent( "givePortalGun", true )
 
addEventHandler( "resupplyAmmo", getRootElement(),
function( )
giveWeaponAmmo( source, 31, 1 )
end
)
addEvent( "resupplyAmmo", true )

But when I try to trigger these serverevents with the Clientscript, it won't work. :|

thisPlayer = getLocalPlayer( )
triggerServerEvent ( "resupplyAmmo", thisPlayer ) 
--And
triggerServerEvent ( "givePortalGun", thisPlayer )

Link to comment

Thanks :D

But when one problem is fixed, another will be created..

Why does my elegy not replace?

Clientscript:

function replaceModels()
outputChatBox("Replacing...") --I even don't get this message 
--Nissan Skyline into Elegy
txd = engineLoadTXD ( "elegy.txd" )
engineImportTXD ( txd, 562 )
dff = engineLoadDFF ( "elegy.dff", 562 )
engineReplaceModel ( dff, 562 )
end
addEventHandler("onClientPlayerJoin", getRootElement(), replaceModels)

Meta:

<meta>
<info author="theguywhomadethemodels" version="1.0.0" type="script" />
<script src="import.lua" type="client" />
<file src="elegy.txd" />
<file src="elegy.dff" />
</meta>

Link to comment

Because the event is triggered for players who join the server but not player who joined the server.

This event is triggered when a player joins a server. It is triggered for all players other than the local player.

Use onClientResourceStart instead which is triggered when resource starts and after all files has been downloaded by the player when (s)he joined the server.

I'm still wondering why people don't use wiki as their MTA bible. That's place where they can find everything they need to know about scripting for MTA.

Link to comment
I'm still wondering why people don't use wiki as their MTA bible. That's place where they can find everything they need to know about scripting for MTA.

I AM using the wiki. But this was just a little mistake in reading. Don't be mad :P

But why are the collissions of custom planes and boats not working? Isn't that implemented yet? (I haven't found something about this)

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...