[DiscordArchive] LF help with script which using item about tele...
[DiscordArchive] LF help with script which using item about tele...
Archived author: Needle • Posted: 2024-12-22T16:35:25.121000+00:00
Original source
Archived author: Needle • Posted: 2024-12-22T16:35:26.768000+00:00
Original source
Thread automatically created by Funnye in <#415944535718494208>
Archived author: Funnye • Posted: 2024-12-22T16:35:39.920000+00:00
Original source
thats the script which i am using
Custom_ITEM_Friend_Porter.lua
![[Image: image.png?ex=690c25be&is=690ad43e&hm=439...e0fbb0f13&]](https://cdn.discordapp.com/attachments/1320429498574966856/1320429706134163568/image.png?ex=690c25be&is=690ad43e&hm=439b5e482a0de8e1f912e3200667a9a4f9bd1b01552e76b4b89a3f1e0fbb0f13&)
Archived author: Funnye • Posted: 2024-12-22T16:36:14.408000+00:00
Original source
working on 50% but when u click at the options got that error : scripts\Custom_ITEM_Friend_Porter.lua:42: attempt to call method 'GetMapId' (a nil value)
![[Image: image.png?ex=690c25be&is=690ad43e&hm=439...e0fbb0f13&]](https://cdn.discordapp.com/attachments/1320429498574966856/1320429706134163568/image.png?ex=690c25be&is=690ad43e&hm=439b5e482a0de8e1f912e3200667a9a4f9bd1b01552e76b4b89a3f1e0fbb0f13&)
Archived author: Deleted User • Posted: 2024-12-22T16:47:51.324000+00:00
Original source
Running the code through ChatGPT returns potential fixes
Archived author: Deleted User • Posted: 2024-12-22T16:48:28.224000+00:00
Original source
The error message indicates that the GetMapId method is being called on a nil value, which means it's trying to access an object or table that doesn't exist.
In your case, this issue arises when you're checking if targetPlayer has a valid map ID. The problem lies in these lines:
```
mapId = targetPlayer:GetMapId() -- Attempt to retrieve map ID again
if not mapId then
print("Error: Failed to retrieve map ID from target player. Defaulting to player's current map ID.")
mapId = player:GetMapId()
print("Using Player's Current Map ID:", mapId) -- Debug
end
mapId = targetPlayer:GetMapId() -- This line is unnecessary and causes the error.```
You're trying to call GetMapId on both targetPlayer (which doesn't exist in this context, as you've already retrieved it with player:GetClosestPlayer() or player:GetRandomPlayer(0)) and then again on targetPlayer. This is causing the error because targetPlayer is nil.
Here's how to fix your code:
Archived author: Deleted User • Posted: 2024-12-22T16:48:43.864000+00:00
Original source
```
-- Function to handle menu selection
function Item_OnSelect(item, event, player, sender, intid, code)
print("Item_OnSelect called with intid:", intid) -- Debug
local targetPlayer, mapId, x, y, z, o
if intid == 1 then
print("Teleporting to closest player") -- Debug
targetPlayer = player:GetClosestPlayer()
elseif intid == 2 then
print("Teleporting to random nearby player") -- Debug
targetPlayer = player:GetRandomPlayer(0)
elseif intid == 3 then
print("Player canceled the menu") -- Debug
player:GossipComplete()
return
end
if not targetPlayer then
print("No players nearby") -- Debug
player:SendAreaTriggerMessage("No players nearby to teleport to.")
player:GossipComplete()
return
end
x, y, z = targetPlayer:GetX(), targetPlayer:GetY(), targetPlayer:GetZ()
o = targetPlayer:GetO()
mapId = targetPlayer:GetMapId() -- Get the map ID from the retrieved target player.
if not mapId then
print("Error: Failed to retrieve map ID. Defaulting to player's current map ID.")
mapId = player:GetMapId()
print("Using Player's Current Map ID:", mapId) -- Debug
end
if not x or not y or not z or not o then
print("Error: Invalid target player position data") -- Debug
player:SendAreaTriggerMessage("Failed to retrieve target player position data.")
player:GossipComplete()
return
end
if not player:HasItem(20857) then
print("Player does not have the required item") -- Debug
player:SendAreaTriggerMessage("You don't have the required item (20857) to teleport.")
player:GossipComplete()
return
end
player:RemoveItem(20857, 1)
player:Teleport(mapId, x, y, z, o)
player:SendAreaTriggerMessage("You have been teleported to the target. Item 20857 has been consumed.")
player:GossipComplete()
end```
Archived author: Deleted User • Posted: 2024-12-22T16:48:45.442000+00:00
Original source
This revised code retrieves targetPlayer once and uses its properties as needed without trying to call methods on it again.
Archived author: Funnye • Posted: 2024-12-22T17:13:27.597000+00:00
Original source
still nothing hm
Archived author: Needle • Posted: 2024-12-22T19:08:55.163000+00:00
Original source
Thread was archived by Funnye. Anyone can send a message to unarchive it.