Jump to content

[Question] What's been plaguing me


Crook4Money

Recommended Posts

I asked this question not even a week ago, but might as well ask it again since it's been plaguing all of my scripts. The question was "how do I stop a script from repeating before it's finished." I didn't give any details as to what it was, but I was trying to make a custom siren for police vehicles. I've been working on it for literally weeks and still can't find a solution and have remade the script several times over it trying to figure out how to stop it. This is what I have so far if you're interested.

function startMySound()
	local driver = getVehicleOccupant ( source )
    if ( driver ) then
		sound = playSound( "sound1.mp3", true )
	end
end
addEventHandler( "onClientVehicleEnter", root, startMySound )
addCommandHandler ( "siren", startMySound)

function stopMySound()
    stopSound( sound )
end
addCommandHandler ( "stopsound", stopMySound )
addEventHandler( "onClientVehicleStartExit", root, stopMySound )

function bindTheKeys ()
  bindKey ( "1", "up", startMySound )   
  bindKey ( "2", "up", stopMySound )     
end
addCommandHandler ( "bindme", bindTheKeys )
addEventHandler("onClientResourceStart", resourceRoot, bindTheKeys)

function disableSirens ( theVehicle, seat )
    local id = getElementModel ( theVehicle )
    if id == 596 or id == 598 or id == 597 then
        toggleControl ( "horn", false )
    else 
        toggleControl ( "horn", true )
    end
end
addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), disableSirens )

The sound just goes on top of eachother everytime it is activated and I don't know how to prevent it. This script is far from complete, but just trying to make out my question clearly this time around with video this time .:)

 

The script I'm working on and also having this problem with is my toll both script. I've spent almost a week on this as well and barely got it functioning correctly today, but I'm still having the same problem with no solution. The main command activates over and over again and I don't know how to prevent it and can't find a solution. 

local gate1 = createObject(968, 1544.6999511719, -1623.9000244141, 13, 0, 89.1, 270)
local marker1 = createMarker(1544.5, -1628, 13.5, "cylinder",6,225,225,255,25)

 function moveToll(player)
local player
if source == marker1 then
	moveObject(gate1, 2000, 1544.6999511719, -1623.9000244141, 13, 0, -89.1, 0)
	if moveObject then setTimer(gate2, 4000, 1, true) 
	end
end
end
addEventHandler("onClientMarkerHit", root, moveToll)

function gate2()
moveObject(gate1, 2000, 1544.6999511719, -1623.9000244141, 13, 0, 89.1, 0)
end

 

 

Edited by Crook4Money
Link to comment
  • Moderators

I think you should learn about booleans.
Do you know what they are for?


Quick awnser: true/false

They are used for statuses. You can consider true as yes and false as no.

I recommend you to do some research on Google for a better understanding. Once you understand the concept of booleans you can solve your problem.

Link to comment
local gate1 = createObject(968, 1544.6999511719, -1623.9000244141, 13, 0, 89.1, 270)
local marker1 = createMarker(1544.5, -1628, 13.5, "cylinder",6,225,225,255,25)
local isMoving = false
local moveTime = 2000

function moveToll(player)
	if isMoving then return end
	if source == marker1 then
		local move = moveObject(gate1, moveTime, 1544.6999511719, -1623.9000244141, 13, 0, -89.1, 0)
		if move then 
			isMoving = true
			setTimer(gate2, 4000, 1, true)
		end
	end
end
addEventHandler("onClientMarkerHit", root, moveToll)

function gate2()
	moveObject(gate1, moveTime, 1544.6999511719, -1623.9000244141, 13, 0, 89.1, 0)
	setTimer(function() 
		isMoving = false
	end, moveTime, 1)
end

As for second problem, this should work, i haven't tested it, but you can see how this could be done. You have to save the current movement state in variable, and move the barrier only if it isn't already moving. You cant use moveObject as condition because it's function, it's gonna always return true. You can see that i save function result in variable "move" and check it in if afterwards. The variable "isMoving" is saving current state of movement. the statement "if isMoving then return end" makes it so the function doesn't start if the barrier is currently moving.

  • Thanks 1
Link to comment

Thanks. I know booleans = true/false, but I'm still not familiar when you can use them outside of elements, but this gave me a bit of a better understanding as to how that was incorporated to prevent the event from triggering again before finished. The script works and now I am familiar with isMoving as I didn't know that was a thing. Hopefully should help me fix other scripts I run into with this issue.

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