Can you send me your getCastleOwner function?
function onStepIn(cid, item, position)
if getPlayerGuildId(cid) ~= getCastleOwner() then
doPlayerSendCancel(cid, 'Nie masz dostepu, aby sie tam udac!')
doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), true)...