mtasa-blue icon indicating copy to clipboard operation
mtasa-blue copied to clipboard

attachElements is not working properly when used in DM (race gamemode)

Open PrimelPrime opened this issue 2 years ago • 3 comments

Describe the bug

Whenever you try and attachElements to the localPlayer on a clientside script it will display and attach the elements correctly BUT only this one time after that your whole function becomes broken and is not useable anymore the only way to fix this is to either edit the meta.xml or delete local map files entirely and you can reproduce it over and over again

There does not seem to be a clear indicator as to why this happens I've tried several variations to attachElements to the vehicle of the localPlayer but it does only work this one time and after that wont no more

https://www.youtube.com/watch?v=0B2247Bju-s

Steps to reproduce

First write an attachElements script

local playerVehicle = getPedOccupiedVehicle(localPlayer)
local halo = createObject(3438, 0, 0, 0, 0, 0, 0)
local marker = {}
local lights = {}

function attachHalo()
    if playerVehicle then
        local radius = 0.4
        attachElements(halo, playerVehicle, 0, -0.1, 0.85, 0, 0, 0)
        setElementDoubleSided(halo, true)
        setElementCollisionsEnabled(halo, false)
        for i = 1, 20 do
            local angle = math.rad((360/20) * i)
            local markerX = radius * math.sin(angle)
            local markerY = radius * math.cos(angle)
            local size = 0.1
            if i <= 5 then
                size = 0.125
            elseif i <= 10 then
                size = 0.1
            elseif i <= 15 then
                size = 0.125
            else
                size = 0.1
            end
            marker[i] = createMarker(0, 0, 0, "corona", size, 255, 244, 61, 255)
            lights[i] = createLight(0, 0, 0, 0, 1, 255, 244, 61, 0, 0, 0)
            attachElements(marker[i], playerVehicle, markerX, markerY-0.1, 0.85, 0, 0, 0)
            attachElements(lights[i], playerVehicle, markerX, markerY-0.1, 0.85, 0, 0, 0)
        end
    end
end
addEventHandler("onClientResourceStart", resourceRoot, attachHalo)

function removeHalo()
    if playerVehicle and getElementModel(playerVehicle) == 425 then
        destroyElement(halo)
        for i = 1, 20 do
            destroyElement(marker[i])
            destroyElement(lights[i])
        end
    end
end
addEventHandler("onClientElementModelChange", root, removeHalo)

function recallHalo()
	if playerVehicle and getElementModel(playerVehicle) == 411 then
		if not isElement(halo) then
			halo = createObject(3438, 0, 0, 0, 0, 0, 0)
			attachHalo()
		end
	end
end
addEventHandler("onClientElementModelChange", root, recallHalo)

add to any of your desired [DM] map

start map and watch it work once

redo map and look into the abyss (maybe someone can find some delicious chocolate there) - see video above

delete local files to repeat

get frustrated and give up

Version

Multi Theft Auto v1.6-release-22684

Additional context

No response

Relevant log output

No response

Security Policy

  • [X] I have read and understood the Security Policy and this issue is not security related.

PrimelPrime avatar Sep 08 '24 22:09 PrimelPrime

The problem is not at all related to attachElements, it's an issue in your script. Your script should work as intended replacing your attachHalo function with:

function attachHalo()
	setTimer(function()
		playerVehicle = getPedOccupiedVehicle(localPlayer)
		if playerVehicle then
			local radius = 0.4
			attachElements(halo, playerVehicle, 0, -0.1, 0.85, 0, 0, 0)
			setElementDoubleSided(halo, true)
			setElementCollisionsEnabled(halo, false)
			for i = 1, 20 do
				local angle = math.rad((360/20) * i)
				local markerX = radius * math.sin(angle)
				local markerY = radius * math.cos(angle)
				local size = 0.1
				if i <= 5 then
					size = 0.125
				elseif i <= 10 then
					size = 0.1
				elseif i <= 15 then
					size = 0.125
				else
					size = 0.1
				end
				marker[i] = createMarker(0, 0, 0, "corona", size, 255, 244, 61, 255)
				lights[i] = createLight(0, 0, 0, 0, 1, 255, 244, 61, 0, 0, 0)
				attachElements(marker[i], playerVehicle, markerX, markerY-0.1, 0.85, 0, 0, 0)
				attachElements(lights[i], playerVehicle, markerX, markerY-0.1, 0.85, 0, 0, 0)
			end
		end
	end, 1000, 1)
end
addEventHandler("onClientResourceStart", resourceRoot, attachHalo)

jjustns avatar Sep 09 '24 21:09 jjustns

The problem is not at all related to attachElements, it's an issue in your script. Your script should work as intended replacing your attachHalo function with:

ok that works but why would I have to use a setTimer inside the function if it should trigger onClientResourceStart? Or is it because the client isnt "streamed" in? I must misunderstand something fundamentally there

PrimelPrime avatar Sep 09 '24 23:09 PrimelPrime

yea onClientResourceStart triggers too fast

PrimelPrime avatar Sep 10 '24 02:09 PrimelPrime